question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to resize Canvas

See original GitHub issue

After resizing the canvas, I am unable to draw any element. No error is raised, simply nothing appears on the canvas.

  render() {
    return (
        <Canvas ref={this.renderCanvas} style={{backgroundColor: "red"}} />
    )
  }

renderCanvas = (canvas) => {

      canvas.width = 400;
      canvas.height = 600;
      
    const context = canvas.getContext('2d');

    context.globalAlpha = 1.0;
    context.fillStyle = 'green';
    context.fillRect(0, 130, 50, 50);
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
omnikroncommented, Nov 18, 2018

I’ve had success with this by

  • using the ref callback to store a reference to the canvas on the component
  • using componentDidMount to resize the canvas initially
  • using componentDidUpdate to draw shapes based on props

You could do the drawing in componentDidMount as well, if you don’t need the canvas to redraw based on user input or whatever. Here’s an example of what I mean:

import React, { Component } from "react";
import { StyleSheet, Text, View, Dimensions } from "react-native";
import Canvas from "react-native-canvas";

const { width, height } = Dimensions.get('window');

export default class CanvasRectangles extends Component {
  componentDidMount() {
    this.canvas.width = width;
    this.canvas.height = height;
  };

  componentDidUpdate(props) {
    const context = this.canvas.getContext("2d");
    context.clearRect(0, 0, this.canvas.width, this.canvas.height);
    props.rectangles.map(rectangle => {
      const { x, y, height, width } = rectangle;
      context.strokeRect(x, y, width, height);
    });
  }

  render() {
    return (
      <View style={styles.container}>
        <Canvas ref={(canvas: any) => (this.canvas = canvas)}/>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    position: "absolute",
    flex: 1,
    elevation: 1,
  }
});
2reactions
gc1508commented, Nov 18, 2018

@omnikron Thanks Oli, your approach worked! @iddan I still believe that this should somehow be clarified/simplified… thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot resize Canvas in edit mode - Unity Forum
Ah yes, I knew that, but thought it was still possible to drag it around temporarily. And it is - but you have...
Read more >
Resizing canvas, where's the problem? - Stack Overflow
If the pictures height becomes to big for the window then the picture should resize itself according to the height and not the...
Read more >
Cannot resize canvas - Figma Community Forum
Hello, I am new to Figma and It's been quite hard to find a way to resize the canvas (like making it longer...
Read more >
Unable to resize Canvas - Lucidchart Help
First, if you ensure Auto-Tiling is turned off in the Page Settings menu, tabs will appear in the corner of your document which...
Read more >
Canvas size not adjusting correctly - Adobe Support Community
In the Layers panel, drag the layer out from under any Artboards so that it's out on its own. As soon as you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found