0

I have this little JavaScript canvas snippet:

(function() {
    // Creates a new canvas element and appends it as a child
    // to the parent element, and returns the reference to
    // the newly created canvas element


    function createCanvas(parent, width, height) {
        var canvas = {};
        canvas.node = document.createElement('canvas');
        canvas.context = canvas.node.getContext('2d');
        canvas.node.width = width || 100;
        canvas.node.height = height || 100;
        parent.appendChild(canvas.node);
        return canvas;
    }

    function init(container, width, height, fillColor) {
        var canvas = createCanvas(container, width, height);
        var ctx = canvas.context;
        // define a custom fillCircle method
        ctx.fillCircle = function(x, y, radius, fillColor) {
            this.fillStyle = fillColor;
            this.beginPath();
            this.moveTo(x, y);
            this.arc(x, y, radius, 0, Math.PI * 2, false);
            this.fill();
        };
        ctx.clearTo = function(fillColor) {
            ctx.fillStyle = fillColor;
            ctx.fillRect(0, 0, width, height);
        };
        ctx.clearTo(fillColor || "#ddd");

        // bind mouse events
        canvas.node.onmousemove = function(e) {
            if (!canvas.isDrawing) {
               return;
            }
            var x = e.pageX - this.offsetLeft;
            var y = e.pageY - this.offsetTop;
            var radius = 10; // or whatever
            var fillColor = '#ff0000';
            ctx.fillCircle(x, y, radius, fillColor);
        };
        canvas.node.onmousedown = function(e) {
            canvas.isDrawing = true;
        };
        canvas.node.onmouseup = function(e) {
            canvas.isDrawing = false;
        };
    }

    var container = document.getElementById('canvas');
    init(container, 200, 200, '#ddd');

})();

Now it takes the div with the id of canvas, and creates a little HTML5 canvas inside it, where the user can basically draw with their mouse. NOW thats nice... but what if I had to save whats inside the canvas with canvas.toDataURL()? As far as I know, I would have to take the canvas, and do the canvas.toDataURL() to save it to an image. But- that needs an ID right? SOOOO when I created the canvas in the createCanvas function, how would I make an id accompanying it, so I could do something like document.getElementById("idofcanvas").toDataURL() ?

Thank you in advance for your help! :)

seanlevan
  • 1,367
  • 3
  • 15
  • 33

1 Answers1

2

But- that needs an ID right?

It doesn't need an ID, but an ID won't hurt (so long as it's actually unique ;-)). Any method to select a DOM element will work here. If there's only one canvas on the page, this would suffice, for example:

var canvas = document.getElementsByTagName('canvas')[0];
var imageData = canvas.toDataURL();

When I created the canvas in the createCanvas function, how would I make an id accompanying it?

You'd just set the ID property of the canvas element before returning it. Remember, you need to ensure that this value is unique!

function createCanvas(parent, width, height) {
    var canvas = {};
    canvas.node = document.createElement('canvas');
    canvas.node.id = /* some unique value here */;
    canvas.context = canvas.node.getContext('2d');
    canvas.node.width = width || 100;
    canvas.node.height = height || 100;
    parent.appendChild(canvas.node);
    return canvas;
}
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • 1
    You could contain your search to `document.getElementById('canvas').getElementsByTagName('canvas')[0])` in case there are multiple canvases on the page – Ruan Mendes Feb 26 '13 at 18:34
  • That's true. I didn't notice that the canvases is appended to a `
    `.
    – Matt Ball Feb 26 '13 at 18:35
  • 1
    OH I just saw the rest of your answer. :) SO cool! :) Didn't know you could just change the id with `canvas.node.id`! :) Amazing! :) It does make sense though, its just like `canvas.node.style` or `canvas.node.value` or something. dumb moment for me! :) Thanks so much! – seanlevan Feb 26 '13 at 18:39
  • @user2049022 I'm not convinced you actually need to generate an ID at all. Where in the code do you want to be able to call `canvas.toDataURL()`? – Matt Ball Feb 26 '13 at 18:44
  • I'm sorry for my stupidity... But I'm encountering a problem here: http://stackoverflow.com/questions/15097068/canvas-html5-javascript-code-not-working-with-canvas-todataurl First time I've ever dealt with canvas. – seanlevan Feb 26 '13 at 18:55