I have a strange bug. I have a React app that is using fabricjs. I can render things onto the canvas just fine, but when I try to save the canvas as an image, a strange bug occurs: after canvas.toDataURL is called, the entire canvas goes blank until the user clicks on one of the elements on the canvas itself, after which all elements will reappear.
The url created by toDataURL works fine: downloading this file will produce the expected image with all of the elements rendered
Here is a fiddle that demonstrates this behavior:
https://jsfiddle.net/db1gc45p/22/
const {useState, useEffect, useRef, useCallback} = React;
const useFabric = (onChange) => {
const fabricRef = useRef();
const disposeRef = useRef();
return useCallback(
(node) => {
if (node) {
fabricRef.current = new fabric.Canvas(node);
if (onChange) {
disposeRef.current = onChange(fabricRef.current);
}
} else if (fabricRef.current) {
fabricRef.current.dispose();
if (disposeRef.current) {
disposeRef.current();
disposeRef.current = undefined;
}
}
},
[onChange]
);
};
function Example() {
const ref = useFabric((canvas) => {
canvas.add(new fabric.Text("hello"))
canvas.toDataURL()
});
return (
<div style={{ display: "inline-block", border: "solid" }}>
<canvas
ref={ref}
width={100}
height={100}
/>
</div>
);
}
ReactDOM.render( <Example />, document.getElementById('root') );
You can see the bug for yourself. By commenting/uncommenting line 30 canvas.toDataURL(), you can see that the canvas element stays visible when we don't call canvas.toDataURL()
However, even if we do call canvas.toDataURL(), we can get the element to re-appear by clicking on it on the canvas.
How can I prevent all of the elements from going invisible?
Fabric.js does some rendering updates asynchronously, similar issue, however you can force a synchronous update (after doing something that triggers the async update) by using canvas.renderAll().
Try
function Example() {
const ref = useFabric((canvas) => {
canvas.add(new fabric.Text("hello"))
canvas.renderAll()
canvas.toDataURL()
});
Related
How to change the background of the page when it loads without having to click the button?
import img1 from '../images/img1.jpg';
import img2 from '../images/img2.jpg';
import img3 from '../images/img3.jpg';
const [background, setBackground] = useState(img1);
const list = [img1, img2, img3];
const css = {
backgroundImage: `url(${background})`,
};
return (
<div style={css}>
<button onLoad={() => setBackground(list)}>Change background</button>
</div>
);
but setBackground(list)} does not read the array and the onLoad event does not work when the page is reloaded.
thanks for any help!
As mentioned above the solution requires localStorage, useEffect, useRef. All logic is inside useEffect, because when the component is mounting all the magic happens here. working example
Initialize an absolute number with useRef
Trying to get value from localStorage, if is empty then we set
default value. And the same place, we checking the array index.
Second time avoid the check and we increace gotted value andabsolute
number.
Then sending result to state and localStorage
App.js
import { useState, useEffect, useRef } from "react";
export default function App() {
const img1 = "https://dummyimage.com/400x200/a648a6/e3e4e8.png";
const img2 = "https://dummyimage.com/400x200/4e49a6/e3e4e8.png";
const img3 = "https://dummyimage.com/400x200/077d32/e3e4e8.png";
let [lsNum, setLsNum] = useState(0);
// Absolute number '1'
let count = useRef(1);
const list = [img1, img2, img3];
useEffect(() => {
// Get value from localStorage, transform to number
const lS = Number(localStorage.getItem("image"));
// Check! If localStorage have number 2 / more
// Set number 0
if (lS >= 2) {
localStorage.setItem("image", 0);
return;
}
// Get absolute number and increase with number from localStorage
count.current = count.current + lS;
// Set result to state
setLsNum(count.current);
// Set the resulting number to localStorage
localStorage.setItem("image", count.current);
}, []);
const css = {
height: "200px",
width: "400px",
display: "block",
backgroundImage: `url(${list[lsNum]})`, // index change
backgroundPosition: "center",
backgroundSize: "cover",
border: "1px solid red"
};
return (
<div className="App">
<div style={css}></div>
</div>
);
}
Hi i'm trying to get the screenX event(e.screenX) when the pointer moves on a container/image.
Once i get the screenX value it should show "i'm Left" if < 50% and show "i'm Right" if >=50%.
I've tried to get the screenX but i'm getting the console.log as "undefined" and not a value.
Below is my code:
import { useEffect, useState } from "react";
function useScreenX() {
const [screenPosition, setScreenPosition] = useState(0);
function handleDocument() {
const { screenX: currentScreenX } = document.querySelector("#screen-log"); // I'm doing something wrong here//
setScreenPosition(currentScreenX);
console.log(currentScreenX);
}
useEffect(() => {
window.addEventListener("mousemove", handleDocument);
return () =>
window.removeEventListener("mousemove", handleDocument);
}, []);
return (
<div id="screen-log">
<h1>My Container</h1>
</div>
);
}
export default useScreenX;
CSS Style:
#screen-log {
background-color: gold;
width: 100vw;
height: 200px;
}
You are almost there. Just few minor corrections to make:
you should use the event in your handleDocument
screenX returns a number and you are trying to store it as an object
you need mouse movements to be tracked on specific div but you are attaching the listener to the entire window.
The handleDocument function automatically get's the event object. Use it to get screenX. Also obtain the element in useEffect and attach the listener to the element.
Working copy of your code is here in the codesandbox
Code Snippet:
export default function App() {
const [screenPosition, setScreenPosition] = useState(0);
function handleDocument(e) {
const screenX = e.screenX;
setScreenPosition(screenX);
console.log(screenX);
}
useEffect(() => {
document
.getElementById("screen-log")
.addEventListener("mousemove", handleDocument);
return () =>
document
.getElementById("screen-log")
.removeEventListener("mousemove", handleDocument);
}, []);
return (
<div id="screen-log">
<h1>My Container</h1>
</div>
);
}
I am attempting to build a React component that displays a 360 degree view of a product. I am attempting to convert this script which uses jQuery to display a draggable 360 degree product image into a React component.
The way this script works is by first loading ~20 images of the product at each angle up to 360 degrees. Then, using jQuery, automatically switching the image based on mouse click and move event.
As a part of my React component I have managed to load the images from a folder and am trying to switch the image based on a mouse click and move event. Additionally, I have found this article which creates a 3D perspective view of an image and has some useful functions for React synthetic events.
How do I use a React synthetic event to capture mouse click and move event so I can switch to the next image in the array?
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
class App extends Component {
render() {
// var foo = new Array(36);
var N = 37;
var imageCount = Array.apply(null, {length: N}).map(Number.call, Number)
imageCount.shift()
console.log(imageCount)
let images = imageCount.map( (name, index) => {
return <img key={index} className="img-responsive" alt="" src={require(`./360-demo/${name}.jpg`)} />
} );
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Welcome to React</h1>
</header>
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
</p>
{images}
</div>
);
}
}
export default App;
Once I get a working component I plan to open source this code so others can also benefit.
You can track the rotation start with onMouseDown, which means the dragging has to originate within the component. For movement and rotation stop, you can use onMouseMove and onMouseUp, but it might be better to attach to the document for that. Generally I wouldn't recommend ever touching the document in React, but with event handlers on the component, it won't detect movement outside of the component (like your jQuery one does).
The gist of it would be something like:
handleMouseDown = event => {
this.setState({
dragging: true,
dragStart: event.screenX,
});
};
handleMouseUp = () => {
this.setState({ dragging: false });
};
handleMouseMove = event => {
if (this.state.dragging) {
const position = event.screenX;
// <-- update your image index
}
};
render = () => {
return (
<div onMouseDown={this.handleMouseDown}>
{this.renderImage()}
</div>
);
};
componentDidMount = () => {
document.addEventListener('mousemove', this.handleMouseMove, false);
document.addEventListener('mouseup', this.handleMouseUp, false);
};
componentWillUnmount = () => {
document.removeEventListener('mousemove', this.handleMouseMove, false);
document.removeEventListener('mouseup', this.handleMouseUp, false);
};
Here is a sandbox with a working example: https://codesandbox.io/s/6v3491kxw3
I'm using react-three-renderer (npm, github) for building a scene with three.js.
I'm having a problem that I've boiled down to an MVCE. Refs aren't updating in the sequence I expect them to. First, here's the main code to look at:
var React = require('react');
var React3 = require('react-three-renderer');
var THREE = require('three');
var ReactDOM = require('react-dom');
class Simple extends React.Component {
constructor(props, context) {
super(props, context);
// construct the position vector here, because if we use 'new' within render,
// React will think that things have changed when they have not.
this.cameraPosition = new THREE.Vector3(0, 0, 5);
this.state = {
shape: 'box'
};
this.toggleShape = this.toggleShape.bind(this);
}
toggleShape() {
if(this.state.shape === 'box') {
this.setState({ shape: 'circle' });
} else {
this.setState({ shape: 'box' });
}
}
renderShape() {
if(this.state.shape === 'box') {
return <mesh>
<boxGeometry
width={1}
height={1}
depth={1}
name='box'
ref={
(shape) => {
this.shape = shape;
console.log('box ref ' + shape);
}
}
/>
<meshBasicMaterial
color={0x00ff00}
/>
</mesh>;
} else {
return <mesh>
<circleGeometry
radius={2}
segments={50}
name='circle'
ref={
(shape) => {
this.shape = shape;
console.log('circle ref ' + shape);
}
}
/>
<meshBasicMaterial
color={0x0000ff}
/>
</mesh>
}
}
componentDidUpdate() {
console.log('componentDidUpdate: the active shape is ' + this.shape.name);
}
render() {
const width = window.innerWidth; // canvas width
const height = window.innerHeight; // canvas height
var position = new THREE.Vector3(0, 0, 10);
var scale = new THREE.Vector3(100,50,1);
var shape = this.renderShape();
return (<div>
<button onClick={this.toggleShape}>Toggle Shape</button>
<React3
mainCamera="camera"
width={width}
height={height}
onAnimate={this._onAnimate}>
<scene>
<perspectiveCamera
name="camera"
fov={75}
aspect={width / height}
near={0.1}
far={1000}
position={this.cameraPosition}/>
{shape}
</scene>
</React3>
</div>);
}
}
ReactDOM.render(<Simple/>, document.querySelector('.root-anchor'));
This renders a basic scene with a green box, a fork of the example on react-three-renderer's github landing page. The button on the top left toggles the shape in the scene to be a blue circle, and if clicked again, back to the green box. I'm doing some logging in the ref callbacks and in componentDidUpdate. Here's where the core of the problem I'm encountering occurs. After clicking the toggle button for the first time, I expect the ref for the shape to be pointing to the circle. But as you can see from the logging, in componentDidUpdate the ref is still pointing to the box:
componentDidUpdate: the active shape is box
Logging in lines after that reveals the ref callbacks are hit
box ref null [React calls null on the old ref to prevent memory leaks]
circle ref [object Object]
You can drop breakpoints in to verify and to inspect. I would expect these two things to happen before we enter componentDidUpdate, but as you can see, it's happening in reverse. Why is this? Is there an underlying issue in react-three-renderer (if so, can you diagnose it?), or am I misunderstanding React refs?
The MVCE is available in this github repository. Download it, run npm install, and open _dev/public/home.html.
Thanks in advance.
I checked the source in react-three-renderer. In lib/React3.jsx, there is a two phased render.
componentDidMount() {
this.react3Renderer = new React3Renderer();
this._render();
}
componentDidUpdate() {
this._render();
}
The _render method is the one that seems to loads the children - the mesh objects within Three.
_render() {
const canvas = this._canvas;
const propsToClone = { ...this.props };
delete propsToClone.canvasStyle;
this.react3Renderer.render(
<react3
{...propsToClone}
onRecreateCanvas={this._onRecreateCanvas}
>
{this.props.children}
</react3>, canvas);
}
The render method draws the canvas and does not populate the children or invoke Three.
render() {
const {
canvasKey,
} = this.state;
return (<canvas
ref={this._canvasRef}
key={canvasKey}
width={this.props.width}
height={this.props.height}
style={{
...this.props.canvasStyle,
width: this.props.width,
height: this.props.height,
}}
/>);
}
Summarizing, this is the sequence:
App Component render is called.
This renders react-three-renderer which draws out a canvas.
componentDidUpdate of App component is called.
componentDidUpdate of react-three-renderer is called.
This calls the _render method.
The _render method updates the canvas by passing props.children (mesh objects) to the Three library.
When the mesh objects are mounted, the respective refs are invoked.
This explains what you are observing in the console statements.
I've got a problem using React JS and canvas together.
everything seems to work smooth, but when render method is called due to changes in other part of my app canvas flicker. It seems that react is re-appending it into the DOM (maybe because I'm playing an animation on it and its drawn frame is different each time).
This is my render method:
render: function() {
var canvasStyle = {
width: this.props.width
};
return (
<canvas id="interactiveCanvas" style={canvasStyle}/>
);
}
As a temporal solution I append my canvas tag manually, without reactjs:
render: function() {
var canvasStyle = {
width: this.props.width
};
return (
<div id="interactivediv3D">
</div>
);
}
And then:
componentDidMount: function() {
var canvasStyle = {
width: this.props.width
};
var el = document.getElementById('interactivediv3D');
el.innerHTML = el.innerHTML +
'<canvas id="interactive3D" style={'+canvasStyle+'}/>';
},
And it works right. But I think this is not a clean way and it's better to render this canvas tag using reactjs.
Any idea of how can I solve the problem? or at least avoiding reactjs re-rendering this canvas when only the drawn frame changed.
I spent time observing with your advice in mind and I find out this flickering was caused by the fact that I was accessing the canvas DOM element and changing canvas.width and canvas.height every time onComponentDidUpdate was called.
I needed to change this property manually because while rendering I don't know canvas.width yet. I fill style-width with "100%" and after rendering I look at "canvas.clientWidth" to extract the value in pixels.
As explained here: https://facebook.github.io/react/tips/dom-event-listeners.html I attached resize event and now I'm changing canvas properties only from my resizeHandler.
Re-rendering the entire tree shouldn't cause your canvas to flicker. But if you think that re-rendering is causing this issue, you could use shouldComponentUpdate lifecycle function and return false whenever you don't want to to update.
https://facebook.github.io/react/docs/component-specs.html#updating-shouldcomponentupdate
So for example:
shouldComponentUpdate: function(nextProps, nextState) {
return false;
}
Any component with the above function defined will not ever run render more than once (at component mounting).
However I've not been able to replicate the flickering issue you complain of. Is your entire tree rendering too often perhaps? In which case the above will fix your canvas, but you should take a look at how many times and how often you are re-rendering your entire tree and fix any issues there.
I had the same problem. I used react-konva library and my component structure looked something like this: canvas had a background image <SelectedImage /> which flicked every time any state property changed.
Initial code
import React, { useState } from "react";
import { Stage, Layer, Image } from "react-konva";
import useImage from "use-image";
function SomeComponent() {
const [imgWidth, setImgWidth] = useState(0);
const [imgHeight, setImgHeight] = useState(0);
//...and other state properties
const SelectedImage = () => {
const [image] = useImage(imgSRC);
if (imgWidth && imgHeight) {
return <Image image={image} width={imgWidth} height={imgHeight} />;
}
return null;
};
return (
<div>
<Stage width={imgWidth} height={imgHeight}>
<Layer>
<SelectedImage />
{/* some other canvas components*/}
</Layer>
</Stage>
</div>
);
}
export default SomeComponent;
The solution
The problem was in a wrong structure of SomeComponent where one component was initialized inside another. In my case it was <SelectedImage /> inside of SomeComponent. I took it out to a separate file and flickering disappeared!
Here is a correct structure
SelectedImage.js
import React from "react";
import { Image } from "react-konva";
import useImage from "use-image";
function SelectedImage({ width, height, imgSRC }) {
const [image] = useImage(imgSRC);
if (width && height) {
return <Image image={image} width={width} height={height} />;
}
return null;
}
export default SelectedImage;
SomeComponent.js
import React, { useState } from "react";
import { Stage, Layer, Image } from "react-konva";
import SelectedImage from "./SelectedImage";
function SomeComponent() {
const [imgWidth, setImgWidth] = useState(0);
const [imgHeight, setImgHeight] = useState(0);
//...and other state properties
return (
<div>
<Stage width={imgWidth} height={imgHeight}>
<Layer>
<SelectedImage width={imgWidth} height={imgHeight} imgSRC={imgSRC} />
{/* some other canvas components*/}
</Layer>
</Stage>
</div>
);
}
export default SomeComponent;