Grafika i animacija u HTML5

    Snoopy




  • HTML

    <!DOCTYPE HTML> <html> <head> <link href="snoopy.css" rel="stylesheet" type="text/css"/> <script src="snoopy.js"></script> </head> <body> <canvas id="canvas" width="1000" height="600"> </canvas> </body> </html>


  • CSS

    #canvas{ background: white; border: 1px solid black; margin-left: 30px; }


  • JScript

    var Animation = function(canvasId){ this.canvas = document.getElementById(canvasId); this.context = this.canvas.getContext("2d"); this.t = 0; this.timeInterval = 0; this.startTime = 0; this.lastTime = 0; this.frame = 0; this.animating = false; // provided by Paul Irish window.requestAnimFrame = (function(callback){ return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(callback){ window.setTimeout(callback, 1000 / 60); }; })(); }; Animation.prototype.getContext = function(){ return this.context; }; Animation.prototype.getCanvas = function(){ return this.canvas; }; Animation.prototype.clear = function(){ this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); }; Animation.prototype.setStage = function(func){ this.stage = func; }; Animation.prototype.isAnimating = function(){ return this.animating; }; Animation.prototype.getFrame = function(){ return this.frame; }; Animation.prototype.start = function(){ this.animating = true; var date = new Date(); this.startTime = date.getTime(); this.lastTime = this.startTime; if (this.stage !== undefined) { this.stage(); } this.animationLoop(); }; Animation.prototype.stop = function(){ this.animating = false; }; Animation.prototype.getTimeInterval = function(){ return this.timeInterval; }; Animation.prototype.getTime = function(){ return this.t; }; Animation.prototype.getFps = function(){ return this.timeInterval > 0 ? 1000 / this.timeInterval : 0; }; Animation.prototype.animationLoop = function(){ var that = this; this.frame++; var date = new Date(); var thisTime = date.getTime(); this.timeInterval = thisTime - this.lastTime; this.t += this.timeInterval; this.lastTime = thisTime; if (this.stage !== undefined) { this.stage(); } if (this.animating) { requestAnimFrame(function(){ that.animationLoop(); }); } }; window.onload = function(){ var anim = new Animation("canvas"); var canvas = document.getElementById("canvas"); var context = canvas.getContext("2d"); var centerX = 0; var centerY = 0; var radius = 11; var startX = 245; var startY = 130; var linearSpeed = 100; var box = { y: 0 }; var dizanje = 1; anim.setStage(function(){ this.clear(); var linearDistEachFrame = linearSpeed * this.getTimeInterval() / 1000; if (dizanje == 1) { box.y += linearDistEachFrame; if(box.y>200){ dizanje = 0; } } if(dizanje == 0){ box.y -= linearDistEachFrame; if(box.y<0){ dizanje =1; } } //Bojanje context.save(); // save state context.translate(canvas.width / 2+220, canvas.height / 2-81); context.rotate(Math.PI / 5); context.scale(6, 6); context.beginPath(); context.arc(centerX, centerY, 17, 0, 2 * Math.PI, false); context.restore(); // restore original state context.fillStyle = "#ffffff"; context.fill(); //Bojanje context.save(); // save state context.translate(canvas.width / 2+120, canvas.height / 2-50); //context.rotate(Math.PI / 4); context.scale(2, 1); context.beginPath(); context.arc(centerX, centerY, 50, 0, 2 * Math.PI, false); context.restore(); // restore original state context.fillStyle = "#ffffff"; context.fill(); //Bojanje context.save(); // save state context.translate(canvas.width / 2+132, canvas.height / 2-38); //context.rotate(Math.PI / 4); context.scale(2, 1); context.beginPath(); context.arc(centerX, centerY, 50, 0, 2 * Math.PI, false); context.restore(); // restore original state context.fillStyle = "#ffffff"; context.fill(); //Bojanje context.save(); // save state context.translate(canvas.width / 2+300, canvas.height / 2-60); context.scale(4, 8); context.beginPath(); context.arc(centerX, centerY, 11, 0, 2 * Math.PI, true); context.restore(); // restore original state context.lineWidth = 20; context.fillStyle = "white"; // line color context.fill(); context.beginPath(); context.moveTo(canvas.width/2+70, canvas.height-64); //13 context.lineTo(canvas.width/2+111, canvas.height-64); context.lineTo(canvas.width/2+111, canvas.height-54); context.lineTo(canvas.width/2+70, canvas.height-54); context.closePath(); context.fillStyle = "white"; context.fill(); context.beginPath(); context.moveTo(canvas.width/2+125, canvas.height-54); //13 context.lineTo(canvas.width/2+265, canvas.height-54); context.closePath(); context.lineWidth = 25; context.strokeStyle = "white"; // line color context.stroke(); context.save(); // save state context.translate(canvas.width / 2+275, canvas.height / 2 + 232); context.rotate(-Math.PI / 2); context.beginPath(); context.arc(centerX, centerY, 26, 0, 2*Math.PI, false); context.restore(); // restore original state context.lineWidth = 10; context.fillStyle = "white"; // line color context.fill(); //trava context.beginPath(); context.moveTo(0, canvas.height-60); //13 context.lineTo(canvas.width, canvas.height-60); context.lineTo(canvas.width, canvas.height); context.lineTo(0, canvas.height); context.closePath(); context.lineWidth = 7; context.strokeStyle = "green"; // line color context.stroke(); context.fillStyle = "green"; context.fill(); //oko context.save(); // save state context.translate(canvas.width / 2+200, canvas.height / 2-100); context.rotate(Math.PI / 5); context.scale(2, 1); context.beginPath(); context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false); context.restore(); // restore original state context.fillStyle = "#000000"; context.fill(); //uho context.save(); // save state context.translate(canvas.width / 2+300, canvas.height / 2-60); context.scale(4, 8); context.beginPath(); context.arc(centerX, centerY, radius, Math.PI, 0, true); context.restore(); // restore original state context.lineWidth = 10; context.strokeStyle = "black"; // line color context.stroke(); //tjeme context.save(); // save state context.translate(canvas.width / 2+300, canvas.height / 2-60); context.scale(4, 4); context.beginPath(); context.arc(centerX-19, centerY, 30, 0, Math.PI+(Math.PI/9), true); context.restore(); // restore original state context.lineWidth = 10; context.strokeStyle = "black"; // line color context.stroke(); //nos context.save(); // save state context.translate(canvas.width / 2+171, canvas.height / 2-44); context.scale(3, 2); context.beginPath(); context.arc(centerX-19, centerY, 30, Math.PI/2, (3*Math.PI)/2, false); context.restore(); // restore original state context.lineWidth = 10; context.strokeStyle = "black"; // line color context.stroke(); //brada context.beginPath(); context.moveTo(614, 316); //13 context.lineTo(720, 319); context.closePath(); context.lineWidth = 10; context.strokeStyle = "black"; // line color context.stroke(); //nos context.save(); // save state context.translate(canvas.width / 2+11, canvas.height / 2-60); context.rotate(Math.PI / 5); context.beginPath(); context.arc(centerX, centerY, 13, 0, 2 * Math.PI, false); context.restore(); // restore original state context.fillStyle = "#000000"; context.fill(); //usta context.save(); // save state context.translate(canvas.width / 2+152, canvas.height / 2-14); context.scale(1, -1); context.beginPath(); context.arc(centerX, centerY, 30, 3*Math.PI/2, 2*Math.PI, false); context.restore(); // restore original state context.lineWidth = 10; context.strokeStyle = "black"; // line color context.stroke(); context.save(); context.translate(canvas.width / 2+188, canvas.height / 2 - 27); context.rotate(Math.PI / 6); context.beginPath(); context.arc(centerX, centerY, 11, 0, Math.PI, false); context.restore(); context.lineWidth = 10; context.strokeStyle = "black"; context.stroke(); //vrat context.beginPath(); context.moveTo(canvas.width / 2+220, canvas.height / 2+19); //13 context.lineTo(canvas.width / 2+220, canvas.height / 2 + 60); context.closePath(); context.lineWidth = 10; context.strokeStyle = "black"; // line color context.stroke(); context.beginPath(); context.moveTo(canvas.width / 2+268, canvas.height / 2+10); //13 context.lineTo(canvas.width / 2+268, canvas.height / 2 + 60); context.closePath(); context.lineWidth = 10; context.strokeStyle = "black"; // line color context.stroke(); //ogrlica context.save(); context.translate(225, 40); context.rect(canvas.width / 2, canvas.height / 2, 45, 10); context.restore(); context.fillStyle = "red"; context.fill(); context.lineWidth = 5; context.strokeStyle = "black"; context.stroke(); //ledja context.beginPath(); context.moveTo(canvas.width / 2+268, canvas.height / 2+60); //13 context.lineTo(canvas.width / 2+305, canvas.height / 2 + 240); context.closePath(); context.lineWidth = 10; context.strokeStyle = "black"; // line color context.stroke(); context.beginPath(); context.moveTo(canvas.width / 2+221, canvas.height / 2+60); //13 context.lineTo(canvas.width / 2+205, canvas.height / 2 +110); context.closePath(); context.lineWidth = 10; context.strokeStyle = "black"; // line color context.stroke(); //lijeva noga context.save(); // save state context.translate(canvas.width / 2+269, canvas.height / 2 + 230); context.rotate(-Math.PI / 2); context.beginPath(); context.arc(centerX, centerY, 35, 0, Math.PI, false); context.restore(); // restore original state context.lineWidth = 10; context.strokeStyle = "black"; // line color context.stroke(); context.beginPath(); context.moveTo(canvas.width / 2+125, canvas.height / 2+265); //13 context.lineTo(canvas.width / 2+265, canvas.height / 2 +265); context.closePath(); context.lineWidth = 10; context.strokeStyle = "black"; // line color context.stroke(); context.beginPath(); context.moveTo(canvas.width / 2+185, canvas.height / 2+205); //13 context.lineTo(canvas.width / 2+270, canvas.height / 2 +195); context.closePath(); context.lineWidth = 10; context.strokeStyle = "black"; // line color context.stroke(); context.save(); // save state context.translate(canvas.width / 2+153, canvas.height / 2+170); context.beginPath(); context.arc(centerX, centerY, 34, 0, Math.PI, true); context.restore(); // restore original state context.lineWidth = 10; context.strokeStyle = "black"; // line color context.stroke(); context.beginPath(); context.moveTo(canvas.width / 2+119, canvas.height / 2+170); //13 context.lineTo(canvas.width / 2+115, canvas.height / 2 +270); context.closePath(); context.lineWidth = 10; context.strokeStyle = "black"; // line color context.stroke(); context.beginPath(); context.moveTo(canvas.width / 2+187, canvas.height / 2+170); //13 context.lineTo(canvas.width / 2+188, canvas.height / 2 +210); context.closePath(); context.lineWidth = 10; context.strokeStyle = "black"; // line color context.stroke(); context.beginPath(); context.moveTo(canvas.width / 2+118, canvas.height / 2+170); //13 context.lineTo(canvas.width / 2+140, canvas.height / 2 +174); context.closePath(); context.lineWidth = 7; context.strokeStyle = "black"; // line color context.stroke(); context.beginPath(); context.moveTo(canvas.width / 2+125, canvas.height / 2+145); //13 context.lineTo(canvas.width / 2+145, canvas.height / 2 +160); context.closePath(); context.lineWidth = 7; context.strokeStyle = "black"; // line color context.stroke(); //bojanje context.beginPath(); context.moveTo(canvas.width/2+122, canvas.height-56); //13 context.lineTo(canvas.width/2+275, canvas.height-54); context.closePath(); context.lineWidth = 29; context.strokeStyle = "white"; // line color context.stroke(); //bojanje context.save(); // save state context.translate(canvas.width / 2+277, canvas.height / 2+240); context.rotate(Math.PI / 5); context.beginPath(); context.arc(centerX, centerY, 20, 0, 2 * Math.PI, false); context.restore(); // restore original state context.fillStyle = "white"; context.fill(); //desna noga context.beginPath(); context.moveTo(canvas.width / 2+108, canvas.height / 2+252); //13 context.lineTo(canvas.width / 2+70, canvas.height / 2 +252); context.closePath(); context.lineWidth = 10; context.strokeStyle = "black"; // line color context.stroke(); context.save(); // save state context.translate(canvas.width / 2+102, canvas.height / 2+170); context.beginPath(); context.arc(centerX, centerY, 34, -Math.PI/6, Math.PI, true); context.restore(); // restore original state context.lineWidth = 10; context.strokeStyle = "black"; // line color context.stroke(); context.beginPath(); context.moveTo(canvas.width / 2+68, canvas.height / 2+173); //13 context.lineTo(canvas.width / 2+68, canvas.height / 2 +250); context.closePath(); context.lineWidth = 10; context.strokeStyle = "black"; // line color context.stroke(); context.beginPath(); context.moveTo(canvas.width / 2+90, canvas.height / 2+169); //13 context.lineTo(canvas.width / 2+67, canvas.height / 2 +167); context.closePath(); context.lineWidth = 7; context.strokeStyle = "black"; // line color context.stroke(); context.beginPath(); context.moveTo(canvas.width / 2+97, canvas.height / 2+154); //13 context.lineTo(canvas.width / 2+79, canvas.height / 2 +145); context.closePath(); context.lineWidth = 7; context.strokeStyle = "black"; // line color context.stroke(); //bojanje context.beginPath(); context.moveTo(canvas.width/2+78, canvas.height-59); //13 context.lineTo(canvas.width/2+106, canvas.height-59); context.closePath(); context.lineWidth = 10; context.strokeStyle = "white"; // line color context.stroke(); //trbuh context.beginPath(); context.moveTo(canvas.width / 2+206, canvas.height / 2+108); //13 context.lineTo(canvas.width / 2+188, canvas.height / 2 +170); context.closePath(); context.lineWidth = 10; context.strokeStyle = "black"; // line color context.stroke(); //lijeva ruka context.save(); // save state context.translate(canvas.width / 2+235, canvas.height / 2+120); context.rotate(-Math.PI / 3); context.scale(7, 1); context.beginPath(); context.arc(centerX, centerY, 10, -Math.PI/6, Math.PI, true); context.restore(); // restore original state context.lineWidth = 10; context.strokeStyle = "black"; // line color context.stroke(); context.beginPath(); context.moveTo(canvas.width / 2+243, canvas.height / 2+180); //13 context.lineTo(canvas.width / 2+198, canvas.height / 2 +180); context.closePath(); context.lineCap = "round"; context.lineWidth = 10; context.strokeStyle = "black"; // line color context.stroke(); context.save(); // save state context.translate(canvas.width / 2 + 245 , canvas.height / 2 +150); context.rotate(Math.PI / 2); context.beginPath(); context.arc(centerX, centerY, 30, 0, Math.PI+Math.PI/6, true); context.restore(); // restore original state context.lineWidth = 10; context.strokeStyle = "black"; // line color context.stroke(); context.beginPath(); context.moveTo(canvas.width / 2+243, canvas.height / 2+160); //13 context.lineTo(canvas.width / 2+243, canvas.height / 2 +175); context.closePath(); context.lineCap = "round"; context.lineWidth = 7; context.strokeStyle = "black"; // line color context.stroke(); context.beginPath(); context.moveTo(canvas.width / 2+256, canvas.height / 2+145); //13 context.lineTo(canvas.width / 2+270, canvas.height / 2 +145); context.closePath(); context.lineCap = "round"; context.lineWidth = 7; context.strokeStyle = "black"; // line color context.stroke(); context.save(); // save state context.translate(canvas.width / 2 + 300 , canvas.height / 2 -60); context.scale(2, 4.7); context.beginPath(); context.arc(centerX, centerY, 17, 0, Math.PI, false); context.restore(); // restore original state context.fillStyle = "black"; context.fill(); //rep context.beginPath(); context.moveTo(canvas.width / 2+300, canvas.height / 2+240); //13 context.lineTo(canvas.width / 2+375, canvas.height / 2 +255); context.lineTo(canvas.width / 2+300, canvas.height / 2 +255); context.closePath(); context.lineCap = "round"; context.lineWidth = 10; context.strokeStyle = "black"; // line color context.stroke(); context.fillStyle = "white"; context.fill(); //desna ruka context.save(); // save state context.translate(canvas.width / 2+185, canvas.height / 2+124); context.rotate(-Math.PI / 3); context.scale(7, 1); context.beginPath(); context.arc(centerX, centerY, 13, -Math.PI/6, -Math.PI/1.8, true); context.restore(); // restore original state context.lineWidth = 10; context.strokeStyle = "black"; // line color context.stroke(); //sunce krug context.save(); // save state context.translate(canvas.width / 2, canvas.height / 2); context.beginPath(); context.arc(centerX-325, centerY-160, 80.0, 0, 2 * Math.PI, false); context.restore(); // restore original state context.fillStyle = "#FFFF00"; context.fill(); context.lineWidth = 7; context.strokeStyle = "black"; // line color context.stroke(); //sunce usta context.save(); // save state context.translate(canvas.width / 2+245, canvas.height / 2+40); context.beginPath(); context.arc(centerX-570, centerY-180, 30.0, 0, Math.PI, false); context.restore(); // restore original state context.lineWidth = 7; context.strokeStyle = "black"; // line color context.stroke(); //lijevo oko sunce context.save(); // save state context.translate(canvas.width / 2-355, canvas.height / 2 - 180); context.beginPath(); context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false); context.restore(); // restore original state context.fillStyle = "#000000"; context.fill(); //desno oko sunce context.save(); // save state context.translate(canvas.width / 2-297, canvas.height / 2 - 180); context.beginPath(); context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false); context.restore(); // restore original state context.fillStyle = "#000000"; context.fill(); //zrake context.beginPath(); context.moveTo(175, 23); //13 context.lineTo(175, 58); context.closePath(); context.lineWidth = 7; context.strokeStyle = "black"; // line color context.stroke(); context.beginPath(); context.moveTo(235, 85); //13 context.lineTo(270, 65); context.closePath(); context.lineWidth = 7; context.strokeStyle = "black"; // line color context.stroke(); context.beginPath(); context.moveTo(258, 140); //13 context.lineTo(293, 140); context.closePath(); context.lineWidth = 7; context.strokeStyle = "black"; // line color context.stroke(); context.beginPath(); context.moveTo(235, 198); //13 context.lineTo(265, 230); context.closePath(); context.lineWidth = 7; context.strokeStyle = "black"; // line color context.stroke(); context.beginPath(); context.moveTo(175, 223); //13 context.lineTo(175, 258); context.closePath(); context.lineWidth = 7; context.strokeStyle = "black"; // line color context.stroke(); context.beginPath(); context.moveTo(115, 198); //13 context.lineTo(80, 230); context.closePath(); context.lineWidth = 7; context.strokeStyle = "black"; // line color context.stroke(); context.beginPath(); context.moveTo(91, 140); //13 context.lineTo(56, 140); context.closePath(); context.lineWidth = 7; context.strokeStyle = "black"; // line color context.stroke(); context.beginPath(); context.moveTo(115, 85); //13 context.lineTo(80, 65); context.closePath(); context.lineWidth = 7; context.strokeStyle = "black"; // line color context.stroke(); //oblak context.save(); context.translate(-100+box.y, 0); context.beginPath(); context.moveTo(startX, startY); context.bezierCurveTo(startX - 40, startY + 20, startX - 40, startY + 70, startX + 60, startY + 70); context.bezierCurveTo(startX + 80, startY + 100, startX + 150, startY + 100, startX + 170, startY + 70); context.bezierCurveTo(startX + 250, startY + 70, startX + 250, startY + 40, startX + 220, startY + 20); context.bezierCurveTo(startX + 260, startY - 40, startX + 200, startY - 50, startX + 170, startY - 30); context.bezierCurveTo(startX + 150, startY - 75, startX + 80, startY - 60, startX + 80, startY - 30); context.bezierCurveTo(startX + 30, startY - 75, startX - 20, startY - 60, startX, startY); context.closePath(); context.restore(); // add a radial gradient var grdCenterX = 260; var grdCenterY = 80; var grd = context.createRadialGradient(grdCenterX, grdCenterY, 10, grdCenterX, grdCenterY, 200); grd.addColorStop(0, "#8ED6FF"); // light blue grd.addColorStop(1, "#004CB3"); // dark blue context.fillStyle = grd; context.fill(); context.lineWidth = 5; context.strokeStyle = "#0000ff"; context.stroke(); //šilterica context.save(); // save state context.translate(canvas.width / 2+255, canvas.height / 2-140); context.rotate(Math.PI / 7); context.beginPath(); context.arc(centerX, centerY, 90.0, 0, Math.PI, true); context.lineTo(169, 0); context.closePath(); context.restore(); // restore original state context.lineWidth = 10; context.strokeStyle = "black"; // line color context.stroke(); context.fillStyle = "#ff0000"; context.fill(); context.save(); // save state context.translate(canvas.width / 2+300, canvas.height / 2-227); context.rotate(Math.PI / 7); context.beginPath(); context.arc(centerX, centerY, 10.0, 0, Math.PI, true); context.closePath(); context.restore(); // restore original state context.lineWidth = 10; context.strokeStyle = "black"; // line color context.stroke(); context.fillStyle = "#ffffff"; context.fill(); //kucica-krov context.beginPath(); context.moveTo(170, 300); //13 context.lineTo(320, 300); context.lineTo(340, 430); context.lineTo(150, 430); context.closePath(); context.lineWidth = 4; context.strokeStyle = "black"; // line color context.stroke(); context.fillStyle = "#bb0000"; context.fill(); //kuæica-crte na krovu context.beginPath(); context.moveTo(160, 350); //13 context.lineTo(330, 350); context.closePath(); context.lineWidth = 2; context.strokeStyle = "black"; // line color context.stroke(); context.beginPath(); context.moveTo(155, 390); //13 context.lineTo(335, 390); context.closePath(); context.lineWidth = 2; context.strokeStyle = "black"; // line color context.stroke(); context.beginPath(); context.moveTo(151, 423); //13 context.lineTo(340, 423); context.closePath(); context.lineWidth = 2; context.strokeStyle = "black"; // line color context.stroke(); //kuæica-zidovi context.beginPath(); context.moveTo(170, 434); //13 context.lineTo(320, 434); context.lineTo(320, 535); context.lineTo(170, 535); context.closePath(); context.lineWidth = 4; context.strokeStyle = "black"; // line color context.stroke(); context.fillStyle = "#bb0000"; context.fill(); //zidovi-pruge context.beginPath(); context.moveTo(170, 460); //13 context.lineTo(320, 460); context.closePath(); context.lineWidth = 2; context.strokeStyle = "black"; // line color context.stroke(); //zidovi-pruge context.beginPath(); context.moveTo(170, 490); //13 context.lineTo(320, 490); context.closePath(); context.lineWidth = 2; context.strokeStyle = "black"; // line color context.stroke(); }); anim.start(); };