Javascript canvas graphics

Slides:



Advertisements
Similar presentations
Bring Life to Your Web Pages with JavaScript and HTML5 Part 2 Ole Ildsgaard Hougaard -
Advertisements

HTML5 CANVAS.  Rectangles  Arcs  Lines  Features we won’t look at  Images  Drag and drop  Animation  Widely supported DRAWING PICTURES IN HTML.
The Document Object Model (DOM) 1 JavaScript is an object-based language—that is, it’s based on manipulating objects by changing each object’s properties.
Web Applications HTML5 Games Windows 8 HTML Apps Basic Web Pages JavaScript Execution Speed DOM Interactions Accelerated Graphics Page Load Time.
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
Using Bitmap graphics Having looked at vector graphics in the canvas, we will now look at using bitmap graphics.
Neal Stublen Why Use a Canvas?  Flexibility… Use of drawing primitives (lines, arcs, text) Direct access to element display pixels.
HTML 5 Previous version: HTML4.01, 1999 Still work in progress, most browsers support some of it.
Programming games More drawing. Text. Radian measure. Faces. Homework: Do your own drawings. Create index.html file. Upload work.
CANVAS Fundamentals. What is Canvas? Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from Canvas is.
Director of Computer Center, DaYeh University Ku-Yaw Chang.
IT Engineering I Instructor: Rezvan Shiravi
INT222 – Internet Fundamentals Weekly Notes: AJAX and Canvas 1.
Web Design Vocab 12 The Last one! Applet, HTTPS, RGB Color, Rollover, Server.
HTML, HTML5 Canvas, JavaScript PART 3 LOOKING MORE CLOSELY AT FULL BLOWN GAME DESIGN A PATTERNED BACKGROUND (CREATED WTIH LOOPS) WITH A MOVING OBJECT CHALLENGES.
CHAPTER 2 HTML & HTML5 II อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
JavaScript Lecture 6 Rachel A Ober
Getting Started with Canvas IDIA Spring 2013 Bridget M. Blodgett.
HTML 5 Tutorial Chapter 5 Canvas. Canvas The tag is used to display graphics. The Canvas is a rectangular area we control each and every pixel of it.
Canvas academy.zariba.com 1. Lecture Content 1.What is Canvas? 2.Including Canvas in your HTML 3.Commands in Canvas 4.Drawing Shapes with Canvas 5.Animations.
Session: 17. © Aptech Ltd. 2Canvas and JavaScript / Session 17  Describe Canvas in HTML5  Explain the procedure to draw lines  Explain the procedure.
Macromedia Dreamweaver 4.0 INTERFACE This presentation will run automatically.
Element. The element Used to dynamically draw graphics using javascript. Capable of drawing paths, circles, rectangles, text, and images.
Programming Games Reprise on drawing on canvas. Jargon (concepts): objects. Demonstrate slingshot. Mouse events. Work on your cannonball. Homework: Finish.
CS 174: Web Programming October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Creating Graphics for the Web Learning & Development Team Telerik Software Academy.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
CSD 340 (Blum)1 Drop-down list and timers. CSD 340 (Blum)2 Double click on the drop-down list icon to place one on the page.
Miss. Ajsa’s Geometry shapes practice Click the arrow button to begin.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
Programming games Context of what we are doing. Drawing on canvas. Homework: [Complete coin toss examples.] Do your own drawings. Upload files to website.
Jaana Holvikivi 1 Introduction to Javascript Jaana Holvikivi Metropolia.
Programming games Review concepts. Crooked coin toss. Drawing on canvas. Homework: Complete [static] drawings. Upload files to website.
Document Object Model Nasrullah. DOM When a page is loaded,browser creates a Document Object Model of the Page.
PART TWO Electronic Color & RGB values 1. Electronic Color Computer Monitors: Use light in 3 colors to create images on the screen Monitors use RED, GREEN,
School of Computing and Information Systems RIA Animation, part I.
Lab 5: More on Events and Introduction to the Canvas Tag User Interface Lab: GUI Lab Sep. 23 rd, 2013.
07 – HTML5 Canvas (1) Informatics Department Parahyangan Catholic University.
Internet & World Wide Web How to Program, 5/e.  JavaScript events  allow scripts to respond to user interactions and modify the page accordingly  Events.
HTML 5 (Part 1) – Start from SCRATCH. HTML 5 – Start from SCRATCH.
Programming games Show work on site. Work on slide show. Timed event for bouncing ball. Homework: [Finish slide show and upload to site.] Acquire a short.
HTML5 ProgLan HTML5 Making a Game on the Canvas Part 1.
THE MOUSE Left Click THE MOUSE Right Click.
Game Development with JS and Canvas 2D
That Gauge is COOL! 
Build in Objects In JavaScript, almost "everything" is an object.
What is a Function Expression?
Internet of the Past.
CMPE 280 Web UI Design and Development September 12 Class Meeting
Introduction to JavaScript Events
Canvas Notes
Cascading Style Sheets
Revision.
מדעי המחשב ורובוטיקה בחט"ב
מדעי המחשב ורובוטיקה בחט"ב
ISC440: Web Programming II Ch14: HTML5 Canvas
The Canvas.
>> Dynamic CSS Selectors
JavaScript.
Drawing Graphics in JavaScript
CSc 337 Lecture 21: Canvas.
What Color is it?.
Context of what we are doing.
Graphics Canvas.
CSc 337 Lecture 20: Canvas.
Color Box Button - Gray Type : object Type : object Type : object
JavaScript – Let’s Draw!
CSc 337 Lecture 19: Canvas.
Detect keyboard ASCII codes
Presentation transcript:

Javascript canvas graphics Step 1: Find the Canvas Element This is done by using the HTML DOM method getElementById(): var canvas = document.getElementById("myCanvas"); Step 2: Create a Drawing Object The getContext() returns a built-in HTML object, CanvasRenderingContext2D, with properties and methods for drawing in 2D. var ctx = canvas.getContext("2d"); Step 3: Draw on the Canvas Draw a rectangle Set the fill style of the drawing object to the color red: ctx.fillStyle = "#FF0000"; The fillRect(x,y,width,height) method draws a rectangle, filled with the fill style, on the canvas: ctx.fillRect(0,0,150,75); Draw a line Draw a circle ctx.moveTo(0,0); ctx.beginPath(); ctx.lineTo(200,100); ctx.arc(95,50,40,0,2*Math.PI); ctx.stroke(); ctx.stroke();

drawCoordinates(x,y) draws red circle at (x,y) <!DOCTYPE HTML> <canvas id= "canvas" width="600px" height="300px"></canvas> <script> var pointSize = 10; drawCoordinates(canvas.width/2, canvas.height/2); function drawCoordinates(x,y){ var c = document.getElementById("canvas").getContext("2d"); c.fillStyle = ‘red’; // Red color c.beginPath(); c.arc(x, y, pointSize, 0, Math.PI * 2, true); c.fill(); } </script> </html> drawCoordinates(x,y) draws red circle at (x,y)

Mouse click draws red circle <!DOCTYPE HTML> <canvas id= "canvas" width="600px" height="300px"></canvas> <script> var pointSize = 3; document.getElementById("canvas").addEventListener("click", getPosition); function getPosition(event){ var x = event.clientX ; var y = event.clientY; drawCoordinates(x,y); } function drawCoordinates(x,y){ var c = document.getElementById("canvas").getContext("2d"); c.fillStyle = ’red’; // Red color c.beginPath(); c.arc(x, y, pointSize, 0, Math.PI * 2, true); c.fill(); </script> </html> Mouse click draws red circle

<!DOCTYPE HTML> <canvas id= "canvas" width="600px" height="300px"></canvas> <script> var pointSize = 3; var culoare=['red',yellow','green']; document.getElementById("canvas").addEventListener("click", getPosition); function getPosition(event){ var x = event.clientX ; var y = event.clientY; var b = event.button; drawCoordinates(x,y,b); } function drawCoordinates(x,y,b){ var c = document.getElementById("canvas").getContext("2d"); //document.write(Math.random()*3); c.fillStyle = culoare[b]; // b=0 red color left button // b=1 yello color middle button // b=2 green color right button c.beginPath(); c.arc(x, y, pointSize, 0, Math.PI * 2, true); c.fill(); </script> </html>

document. getElementById("canvas") document.getElementById("canvas").addEventListener(“mousemove", getPosition); document.getElementById("canvas").addEventListener("mousemove", getPosition); document.getElementById("canvas").addEventListener("click", changeSize); function changeSize(){ pointSize+=1; } //red green blue - integer numbers bwtween 0 and 255 function RGB ( red, green, blue){ var c =0x1000000+ blue + 0x100 * green + 0x10000 *red ; return '#'+c.toString(16).substr(1); r=x%256; g=(y)%256; b=(x*y)%256; c.fillStyle=RGB (r, g, b);

<!DOCTYPE HTML> <body> <canvas id="canvas" width="600px" height="300px"></canvas> <script> var pointSize = 3; document.getElementById("canvas").addEventListener("mousedown", function(){getPosition(event, 'red'); } ); document.getElementById("canvas").addEventListener("mouseup", function(){getPosition(event, 'yellow'); } ); function getPosition(event, color ){ var x = event.clientX; var y = event.clientY; drawCoordinates(x,y, color ); } function drawCoordinates(x,y,color){ var c = document.getElementById("canvas").getContext("2d"); c.fillStyle=color; c.beginPath(); c.arc(x, y, pointSize, 0, Math.PI * 2, true); c.fill(); </script> </body> </html>

document. getElementById("canvas") document.getElementById("canvas").addEventListener("mousedown", function(e){color='red';} ); document.getElementById("canvas").addEventListener("mouseup", function(e){color='yellow';} ); document.getElementById("canvas").addEventListener("mousemove", function(e){getPosition(e,color);} ); document.getElementById("canvas").addEventListener("mousedown", getPosition ); function drawCoordinates(x,y){ var c = document.getElementById("canvas").getContext("2d"); c.fillStyle=color; c.beginPath(); c.moveTo(0,0); c.lineTo(x, y); c.stroke(); }

document. getElementById("canvas") document.getElementById("canvas").addEventListener("mousedown", doMouseDown); document.getElementById("canvas").addEventListener("mouseup", doMouseUp); var c = document.getElementById("canvas").getContext("2d"); function doMouseDown(event) { x=event.pageX; y=event.pageY; c.beginPath(); c.moveTo(x,y); } function doMouseUp(event) { c.lineTo(x, y); c.stroke();

document. getElementById("canvas") document.getElementById("canvas").addEventListener("mousedown", doMouseDown); var i=0; function doMouseDown(event) { var c = document.getElementById("canvas").getContext("2d"); x=event.pageX; y=event.pageY; i++; i%=2; if(i==1){ c.beginPath(); c.moveTo(x, y); } if(i==0){ c.lineTo(x, y); c.stroke(); c.closePath(); If var i =true boolean write the code!

var i=0,x1,y1; function doMouseDown(event) { var c = document.getElementById("canvas").getContext("2d"); x=event.pageX; y=event.pageY; i++; i%=2; if(i==1){ x1=x;y1=y; } if(i==0){ c.fillStyle='red'; c.rect(x1, y1, x-x1, y-y1); c.fill();

var i=0,x1,y1; function doMouseDown(event) { x=event.pageX; y=event.pageY; if(i==0){ x1=x;y1=y; i=1; } if(ii==1 && (x!=x1 && y!=y1) ){ var c = document.getElementById("canvas").getContext("2d"); c.fillStyle='red'; c.rect(x1, y1, x-x1, y-y1); c.fill(); i=0;

function doMouseDown(event) { var c = document.getElementById("canvas").getContext("2d"); vx=event.pageX/10; vy=(canvas.height-event.pageY)/10; x=0,y=canvas.height, g=1,dt=1; //deseneaza o traiectorie parabolica for(i=0;i<100;i++) { c.beginPath(); c.moveTo(x,y); x += vx*dt; y += -vy*dt; vx += 0; vy += -g*dt; c.lineTo(x, y); c.stroke(); }

if( (x-x0)*(x-x0)+ (y-y0)*(y-y0)<r*r ) { alert("Ai nimerit tinta!"); }

<!DOCTYPE HTML> <body> <span id="emo" style="font-size:10px" meta charset="UTF-8"> &#x1F601 U+1F601</span> <p>Copy-paste emoji at !!! symbol</p> <span id="emo2" style="font-size:1em"> !!😁 U+1F601</span> <script> document.addEventListener("click", changeSize); var q=0x1F601; function changeSize(event){ document.getElementById("emo").style.fontSize= parseFloat(document.getElementById("emo").style.fontSize)+10+"px"; document.getElementById("emo2").style.fontSize= parseFloat(document.getElementById("emo2").style.fontSize)+0.2+"em"; q+=1; document.getElementById("emo").innerText = String.fromCodePoint(q)+"U+"+q.toString(16).toUpperCase(); } </script> </body> </html>

var x=0, y=0, vx=2, vy=2; document.getElementById("canvas").addEventListener("mousedown", doMouseDown); function doMouseDown(event) { vx = (event.pageX-x) / 50; vy = (event.pageY-y) / 50; } function miscare(){ var c = document.getElementById("canvas").getContext("2d"); c.fillStyle = 'white'; c.fillRect(0, 0, 600, 300); c.fillStyle = 'red'; c.fillRect(x, y, 20, 20); //c.font="20px Georgia"; //c.fillStyle='red'; //c.fillText("Hey!"+ String.fromCodePoint(0x1F601), x, y); x += vx; y += vy; setInterval( miscare, 20); Moves with velocity set by mouse relative position