Last Updated: February 25, 2016
·
413
· hpcal1

Canvas Introduction

var canvas = document.getElementById('canvas'), //get Eelement
context = canvas.getContext('2d'); //get context of element

context.lineJoin = 'round'; //set the connecting lines
context.lineWidth = 30; //set the line width

context.font = '24px Helvetica'; //set any text size and font
context.fillText('Click anywhere to erase', 175, 200); // add text

context.strokeStyle = 'goldenrod'; //color of stroke style
context.fillStyle = 'rgba(0,0,225,0.5)'; //color of fill style

context.strokeRect(75, 100, 200, 200); //create stroke element
context.fillRect(325, 100, 200, 200); //create fill element

context.canvas.onmousedown = function(e){ //Event handler
context.clearRect(0, 0, canvas.width, canvas.height); //reduce transparency of all the pixels in canvas to zero
};

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside