save() not working on any canvas?

I'm running 05.js 0.4.6 and am attempting the save() function to download a image from the canvas and am getting the error: Uncaught TypeError: Cannot read property 'toDataURL' of undefined

Example of JS (not my actual javascript being used):

function setup() {

//Creating the webpage interface 
var canvas = createCanvas(1280, 1280);
canvas.parent('container');
canvas.position(0,0);
textSize(14);


//Variable center of screen
centerx = width/2;
centery = height/2; 

//Creating the # of authors input (will be replced with CSV data)
but = createInput("10");
but.position((200-176)/2,10);
save('test.png');
}

This is being drawn within the HTML. Running this just leads to that error. What should I do to fix? Thanks!

Answers

  • edited July 2015

    I was able to make it run it w/o error by moving save() into draw().
    And also by explicitly passing the canvas' reference to it:

    function setup() {
      const cnv = createCanvas(320, 240).position(0, 0);
    
      noLoop();
      background('orange');
    
      but = createInput('Type in here...');
      but.position(200-176 >> 1, 10);
    }
    
    function draw() {
      save(cnv, 'test.png');
    }
    

    Although I still think it's very buggy and should be reported here:
    https://GitHub.com/processing/p5.js/issues

    Don't forget to mention your thread here there! O:-)

Sign In or Register to comment.