Comments (4)
Hey there! The common approach in React for this is to save the ref (canvas) as an instance property so:
handleCanvas = (canvas) => {
this.canvas = canvas
// initial render
}
componentDidUpdate() {
console.log(this.canvas); // <= the ref
// cycle animation
}
Generally you better use componentDidUpdate()
or componentWillUpdate()
for side affects which are not your actual render.
from react-native-canvas.
Thank you, so much for your reply, but I am still unable to get my blue ball to move :)
import React, { Component } from 'react';
import {
View,
Text,
Button,
} from 'react-native';
// Components
import Canvas from 'react-native-canvas';
// Styles
import styles from '../styles/snake/snake.styles';
// Class
export default class SnakeCanvas extends Component {
static navigationOptions = {
title: 'null',
header: null
};
constructor(props) {
super(props);
// this.drawBall = this.drawBall.bind(this);
this.state = {
x: 50,
y: 50,
}
}
componentDidmount() {
// setInterval(draw, 10);
}
componentDidUpdate() {
console.log('CANVAS, DID UPDATE: ', this.canvas);
}
/**
*
* @param {Object} canvas Canvas Object
*/
handleCanvas(canvas) {
this.canvas = canvas;
// const {state} = this.props;
// const {ball} = state;
// const {x, y} = ball;
// const {moveBall} = this.props;
let x = this.state.x;
let y = this.state.y;
this.ctx = this.canvas.getContext('2d');
this.ctx.beginPath();
this.ctx.arc(x, y, 10, 0, Math.PI*2);
this.ctx.fillStyle = "#0095DD";
this.ctx.fill();
this.ctx.closePath();
// moveBall();
this.draw();
}
draw() {
// drawing code
setInterval(() => {
console.log('tick');
this.setState({
x: this.state.x += 2,
y: this.state.y += -2,
})
}, 1000);
console.log('draw');
}
render() {
return (
<Canvas ref={this.handleCanvas.bind(this)}/>
);
}
}
from react-native-canvas.
let x = this.state.x;
let y = this.state.y;
this.ctx = this.canvas.getContext('2d');
this.ctx.beginPath();
this.ctx.arc(x, y, 10, 0, Math.PI*2);
this.ctx.fillStyle = "#0095DD";
this.ctx.fill();
this.ctx.closePath();
should be executed at componentDidUpdate()
from react-native-canvas.
General tips:
- you shouldn't bind
handleCanvas()
atrender()
as it will pass a new function prop to<Canvas />
in eachrender()
. Instead use arrow function methods like this:
class SnakeComponent extends Component {
handleCanvas = () => {
}
render() {
return <Canvas ref={this.handleCanvas} />
}
Because handleCanvas()
is an arrow function this
is already bounded
- You shouldn't use mutating operators on
this.state
properties and shouldn't read from it directly when executingsetState()
so change:
- this.setState({
- x: this.state.x += 2,
- y: this.state.y += -2,
- })
+ this.setState(state => ({
+ x: state.x + 2,
+ y: state.y - 2,
+ })
from react-native-canvas.
Related Issues (20)
- cropping an image HOT 1
- Not able to render image on ios 15.2.1 HOT 3
- Path2D differs from the Canvas API, in that it's not connected to a Canvas element HOT 2
- getcontext of a canvas gives TypeError: canvas.getContext is not a function HOT 2
- Difference in drawImage coordinates and getImageData/putImageData coordinates HOT 2
- why Why did I use getImageData() for more than 1 minute? HOT 2
- Canvas remain black on windows
- How do you get fillRect to occupy full dimensions? HOT 4
- ImageData/getImageData/putImageData strange behaviour HOT 2
- error HOT 2
- New architecture support for fabric HOT 1
- require cycle node_modules\react-native-canvas HOT 1
- canvas.loadFromJSON missing in react-native-canvas
- Error: Unable to resolve module ./CanvasRenderingContext2D HOT 1
- Not able to add fontFamily with font
- Application gets hang after 10 mins due to drawing on canvas continuously
- Can't draw image greater than 2048x2048 HOT 1
- Virtual/In-Memory Canvas HOT 1
- Can't draw Image on release HOT 1
- Doesn't work at all with Expo HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native-canvas.