2. /*** CANVAS* */(function() {var my = {};my.name = 'Stijn Van Minnebruggen';my.work = 'Multimedia developer at These Days Antwerp';my.hobbies = 'Passionate about photography, graphic design, typography and music.';my.tweets = 'twitter.com/donotfold';my.website = 'donotfold.be';
3. varabout = {};about.canvas= 'The Canvas element allows dynamic rendering of 2D shapes and bitmap images in the browser'; about.html5 = 'Canvas is part of HTML5 and introduced in 2004';about.how= 'The shapes and images that are drawn on the canvas are scriptable with Javascript';
5. <canvas id="myFirstCanvas" width="300" height="250"> This text is displayed if your browser does not support canvas. </canvas> <p><a href="#" onclick="drawIt();">Click me to draw rectangle</a></p> <script> function drawIt() {varmyFirstCanvas = document.getElementById("myFirstCanvas");var context = myFirstCanvas.getContext("2d");context.fillStyle = 'rgb(255,0,0)';context.fillRect(50, 25, 100, 150); } </script>
6. // basic context methodscontext.fillStyle = "property"; // css color, pattern or gradient (default: black)context.fillRect(x, y, width, height); // draws a rectangle with the current fillStylecontext.strokeStyle = "property"; // allows same properties as fillStylecontext.strokeRect(x, y, width, height); // draws a rectangle with the current strokeStyle(no fill)context.clearRect(x, y, width, height); // clears the pixels in the specified rectangle
7. // strokes // 1. define start point // 2. define end point // 3. set optional styles // 4. draw linecontext.moveTo(x, y); // move the pencil to the starting pointcontext.lineTo(x, y); // imagine a line from the starting point to this ending pointcontext.strokeStyle = "property"; // set same style properties as before if you want context.stroke(); // draw the actual line
9. // gradients // fill or stroke styles allow gradientsvarmyLinearGradient = context.createLinearGradient(x0, y0, x1, y1);varmyRadialGradient = context.createRadialGradient(x0, y0, r0, x1, y1, r1); // make as many color stops as you like, between 0 and 1myLinearGradient.addColorStop(0, "black"); myLinearGradient.addColorStop(1, "white"); // add the gradient to the style propertycontext.fillStyle= myLinearGradient;