react-image-crop will not provide initial crop on canva - reactjs

Have an issue with react-image-crop. If I change the crop, it works perfectly, but if I just load and then hit save, I get an empty image back. At first I thought maybe its just not loading it, but I can see the croped image on load. And I don't get an "undefined" image back, I get an image with no data in it.
This is how I read the image:
useEffect(() => {
console.log("Got completed crop")
const canvas: HTMLCanvasElement = document.getElementById('canvas') as HTMLCanvasElement
if (canvas) {
console.log("Got canvas")
console.log(canvas.toDataURL())
setCroppedImgUrl(canvas.toDataURL())
} else {
}
}, [completedCrop])
This code gets called every time completedCrop is updated, and I can see that it works when the image is first loaded, BUT, the image it returns on load (independent on what image I am cropping) is
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAACWCAYAAABkW7XSAAAAAXNSR0IArs4c6QAABGJJREFUeF7t1AEJAAAMAsHZv/RyPNwSyDncOQIECEQEFskpJgECBM5geQICBDICBitTlaAECBgsP0CAQEbAYGWqEpQAAYPlBwgQyAgYrExVghIgYLD8AAECGQGDlalKUAIEDJYfIEAgI2CwMlUJSoCAwfIDBAhkBAxWpipBCRAwWH6AAIGMgMHKVCUoAQIGyw8QIJARMFiZqgQlQMBg+QECBDICBitTlaAECBgsP0CAQEbAYGWqEpQAAYPlBwgQyAgYrExVghIgYLD8AAECGQGDlalKUAIEDJYfIEAgI2CwMlUJSoCAwfIDBAhkBAxWpipBCRAwWH6AAIGMgMHKVCUoAQIGyw8QIJARMFiZqgQlQMBg+QECBDICBitTlaAECBgsP0CAQEbAYGWqEpQAAYPlBwgQyAgYrExVghIgYLD8AAECGQGDlalKUAIEDJYfIEAgI2CwMlUJSoCAwfIDBAhkBAxWpipBCRAwWH6AAIGMgMHKVCUoAQIGyw8QIJARMFiZqgQlQMBg+QECBDICBitTlaAECBgsP0CAQEbAYGWqEpQAAYPlBwgQyAgYrExVghIgYLD8AAECGQGDlalKUAIEDJYfIEAgI2CwMlUJSoCAwfIDBAhkBAxWpipBCRAwWH6AAIGMgMHKVCUoAQIGyw8QIJARMFiZqgQlQMBg+QECBDICBitTlaAECBgsP0CAQEbAYGWqEpQAAYPlBwgQyAgYrExVghIgYLD8AAECGQGDlalKUAIEDJYfIEAgI2CwMlUJSoCAwfIDBAhkBAxWpipBCRAwWH6AAIGMgMHKVCUoAQIGyw8QIJARMFiZqgQlQMBg+QECBDICBitTlaAECBgsP0CAQEbAYGWqEpQAAYPlBwgQyAgYrExVghIgYLD8AAECGQGDlalKUAIEDJYfIEAgI2CwMlUJSoCAwfIDBAhkBAxWpipBCRAwWH6AAIGMgMHKVCUoAQIGyw8QIJARMFiZqgQlQMBg+QECBDICBitTlaAECBgsP0CAQEbAYGWqEpQAAYPlBwgQyAgYrExVghIgYLD8AAECGQGDlalKUAIEDJYfIEAgI2CwMlUJSoCAwfIDBAhkBAxWpipBCRAwWH6AAIGMgMHKVCUoAQIGyw8QIJARMFiZqgQlQMBg+QECBDICBitTlaAECBgsP0CAQEbAYGWqEpQAAYPlBwgQyAgYrExVghIgYLD8AAECGQGDlalKUAIEDJYfIEAgI2CwMlUJSoCAwfIDBAhkBAxWpipBCRAwWH6AAIGMgMHKVCUoAQIGyw8QIJARMFiZqgQlQMBg+QECBDICBitTlaAECBgsP0CAQEbAYGWqEpQAAYPlBwgQyAgYrExVghIgYLD8AAECGQGDlalKUAIEDJYfIEAgI2CwMlUJSoCAwfIDBAhkBAxWpipBCRAwWH6AAIGMgMHKVCUoAQIGyw8QIJARMFiZqgQlQMBg+QECBDICBitTlaAECBgsP0CAQEbAYGWqEpQAgQdWMQCX4yW9owAAAABJRU5ErkJggg==
To initiate the crop, I use the following code:
<div className='d-flex align-items-center gap-4'>
<div className='text-center'>
<p>Crop:</p>
{Boolean(posterSrc) && (
<ReactCrop
crop={crop}
onChange={(_, percentCrop) => {
setCrop(percentCrop)
}}
onComplete={(crop) => setCompletedCrop(crop)}
aspect={aspect}>
<img
style={{maxHeight: previewHeight, maxWidth: previewWidth, objectFit: "cover"}}
ref={imgRef}
alt="Crop me"
src={posterSrc}
onLoad={onImageLoad}/>
</ReactCrop>
)}
</div>
<div className='text-center'>
<p>Preview:</p>
{Boolean(completedCrop) && (
<canvas
id='canvas'
ref={previewCanvasRef}
style={{
objectFit: 'contain',
width: previewWidth,
height: previewHeight,
}}
/>
)}
</div>
</div>
Anyone have any idea what I'm doing so wrong

Related

React Typescript page - window.print is printing unexpected

I have a react typescript page. It's basically composed of a title and some elements on top, an inner page, and then a side panel with buttons. I was tasked with adding a "print page" to the side panel which should print some third component and open up a print dialogue for it (the window.print for the inner page).
I have 2 problems so far:
when rendering this on the print button, the entire print doesn't work at all (nothing happens). I was able to solve this by setting the onprint to happen on page load. Then it opens the print dialogue on page load but still doesn't work on the print button.
the window.print is printing the entire context of all elements on the page, instead of just new component i'm trying for.
here is part of the code:
const printCoversheet = () => {
setCoversheetData({
documentId: document.id,
somedata: somedata?.data ?? '',,
});
console.log(coversheetData);
};
useEffect(() => {
!isEmpty(coversheetData) && window.print();
window.onafterprint = () => setCoversheetData({} as VerificationErrorCoversheetProps);
}, [coversheetData, setCoversheetData]);
<div css={{ display: 'flex', justifyContent: 'flex-start', marginTop: 16 }}>
<Button
variant="outlined"
size="large"
color="secondary"
onClick={() => printCoversheet()}
>
Print Coversheet
</Button>
</div>
{!isEmpty(coversheetData) && (
<div css={{ display: 'none', '#media print': { display: 'block' } }}>
{!coversheetData.verificationFailures.length ? (
<CoversheetPrint {...coversheetData} />
) : (
<VerificationErrorCoversheet {...coversheetData} />
)}
</div>
)}
I had to do the opposite of this in all other divs
In other divs I did:
window.print() all together didn't seem to work on localhost

React re-render restarting video

I have a react app with some complex view logic. There is one view where I need to have different parents and conditional renders. This causes my stream ref to be lost, or a video to restart. Is there anyway to set this up so my video instance can reach the new view state without restarting?
I've tried display none but it messes with my view, visibility hidden restarts the video.
here's a sandbox of my problem.
https://codesandbox.io/s/romantic-wood-qnn2x0?file=/src/App.js:0-1004
Here's one solution I thought of.
import { useState } from "react";
import React from "react";
import "./styles.css";
let flex = {
display: "flex"
};
let leftParent = {};
let VidInstance = React.forwardRef((props, ref) => {
return (
<div style={{ display: !props.mute ? "flex" : "none", width: 200 }}>
<video
controls=""
preload="none"
autoPlay
src="https://archive.org/download/BigBuckBunny_124/Content/big_buck_bunny_720p_surround.mp4"
width="200"
muted={props.mute}
></video>
</div>
);
});
export default function App() {
let [view, setView] = useState(false);
return (
<div className="App">
<flex style={flex}>
<leftParent style={leftParent}>
<Box />
<Box />
<VidInstance key="vid" mute={view} />
</leftParent>
<VidInstance key="vid" mute={!view} />
</flex>
<button onClick={() => setView(!view)}>view</button>
</div>
);
}
let Box = () => {
let boxStyle = {
width: "200px",
margin: "2px",
height: "100px",
background: "blue"
};
return <div style={boxStyle}></div>;
};
Loading both VidInstance at same time to sync their play time.
Wrap VidInstance with a wrapper div and give that div same width as the video to reserve the empty space when your video goes hidden.
Pass mute prop to VidInstance to mute and hide inactive video.
You have to use the tag <source> inside the video tag... its worked for me:
<video width="320" height="240" controls>
<source src="movie.mp4" type="video/mp4">
<source src="movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>

How to show couple images in preview of ANTD carousel

I was wondering is it possible to show couple images on single preview of ANTD carousel.
For insrance i have a carousel on which i may have different amount of immages.
The problem is that i want to show all avaliable images in preview without heving to schroll through them.
I tried to change size of immages but that didn't help.
Also i haven't found any properties in ANTD for that.
Does anyone know is it possible?
import { Carousel } from 'antd';
const contentStyle = {
height: '160px',
color: '#fff',
lineHeight: '160px',
textAlign: 'center',
background: '#364d79',
};
ReactDOM.render(
<Carousel effect="fade">
<div>
<h3 style={contentStyle}>1</h3>
</div>
<div>
<h3 style={contentStyle}>2</h3>
</div>
<div>
<h3 style={contentStyle}>3</h3>
</div>
<div>
<h3 style={contentStyle}>4</h3>
</div>
</Carousel>,
mountNode,
);
Here's what seemed to work.
Mainly just added an extra item to my array in 0th position and conditionally rendered a preview frame which included all the images in a grid.
Sample:
const images = [...new Array(5).keys()].map((rowIndex, _, array) => {
if (rowIndex === 0) {
return (
<React.Fragment>
<div key={rowIndex} style={previewStyle}>
{array.map((colIndex) => (
<h3 key={(rowIndex + 1) * (colIndex + 1)} style={h3Style}>
Preview: {colIndex}
</h3>
))}
</div>
</React.Fragment>
);
}
return (
<React.Fragment>
<h3 key={rowIndex} style={{...h3Style, lineHeight: '160px',}}>
{rowIndex}
</h3>
</React.Fragment>
);
});

Images wont show up in Nextjs and Strapi project

Currently I'm using NextJs as my frontend and Strapi as my CMS for my web application. I've added the following data to my Citizenship collection type in Strapi:
This is my code in the NextJs side:
export default function Citizenship({ posts }) {
return (
<>
<div style={{textAlign:"center", marginTop:"20px", fontSize:"25px", color:"#0E2043", fontWeight: "500"}}>CITIZENSHIP</div>
<div class="flexbox-container" style={{margin:"70px", marginTop:"0px"}}>
{
posts &&
posts.map((post) => (
<div style={{ padding: "40px" }}>
<div class="citizen-item" key={post.id}>
<div className="container6">
<img
style={{ height: "50%", width: "100%" }}
src={post.Thumbnail.name}
/>
<div style={{textAlign:"center", color:"#E3AB50", padding:"10px", fontSize:"20px"}}>{post.Title}</div>
<div style={{textAlign:"center", color:"#000", padding:"10px", fontSize:"15px"}}>Access to {post.Countries} countries</div>
<div style={{display:"flex", justifyContent:"center", paddingTop:"20px", paddingBottom:"10px"}}>
<button class="findButton">FIND OUT MORE</button>
</div>
</div>
</div>
</div>
))}
</div>
</>
)
}
export async function getStaticProps() {
const res = await fetch('http://localhost:1337/citizenships');
const posts = await res.json();
return {
props: {posts},
}
}
In my output, everything is coming fine except for the Image where it is only showing my first image and all the others are giving a 404. How do I fix this?
Use console.log(post) and find what your post returns in your browser console . If it gives Thumbnail then use it .Or else use what it gives .
use postman for that route with populate query (at the end add "?populate=*") and u will get the complete position of the url of you image and use it .

How to position absolutely positioned element relative to ref position

I've got a bit of a problem here, hope I get help :0
well I am building a pretty little App on React and I have a navbar which includes an absolutely positioned div and two images for now... I have refs on these images and I want this navbar div to be positioned with the same top coordinates upon render...
Here is how I'm trying:
this code sets top state property to refs position:
componentDidMount() {
this.fetchApi();
this.setState({
scrolling: { top: this.first.current.offsetTop },
});
}
this is basically navbar
let { top } = this.state.scrolling;
<div className="map__left-column__logos">
<img
ref={this.first}
onClick={this.linkContainerClick}
className="img-svg"
src={pin}
alt="pingreen"
/>
<img onClick={this.linkContainerClick} src={logo} alt="logo" />
<div
ref={this.myRef}
className="link-container"
style={{ transform, top }}
>
<div className="link-container__upcurve"></div>
<div className="link-container__wrapper"></div>
<a href="#" id="container-href">
{" "}
<img
style={{
top: img1,
transition: "all linear .2s",
}}
className="img-svg"
src={chosenImg}
alt=""
/>
</a>
<div className="link-container__downcurve"></div>
</div>
</div>
</div>
and this is the linKContainerClick function trying to calculate where to scroll after I click the images
linkContainerClick = ({ target }) => {
let id = target.offsetTop;
let pixel = id - this.state.scrolling.top;
this.setState({ innerHtml: target.alt });
this.setState({
scrolling: {
...this.state.scrolling,
transform: `translateY(${pixel}px)`,
},
});
};
The problem is that on the first render of the App the link-container element is misplaced by 20 pixels... after refresh it moves to the right place

Resources