Hosting Solutions
mobile_logo Table of Contents
seal_frontseal_back

Drawing Images and Videos

In this JavaScript exercise you will learn the three different types of parameter settings for drawing images, videos and canvas elements on the canvas element. <!DOCTYPE html> <html> <head> <style> body{ margin:40px; background:#666; } #my_canvas{ background:#FFF; border:#000 1px solid; } </style> <script> var my_pic = new Image(); my_pic.src = "meface.jpg"; function draw(){ var ctx = document.getElementById('my_canvas').getContext('2d'); // drawImage() can take either an HTMLImageElement, an HTMLCanvasElement, // or an HTMLVideoElement as the first parameter // drawImage(image, x, y); // ctx.drawImage(my_pic, 20, 20); // Positioning only // drawImage(image, x, y, w, h); // ctx.drawImage(my_pic, 20, 20, 100, 100); // Positioning and sizing // drawImage(my_pic, clipX, clipY, clipW, clipH, x, y, w, h); ctx.drawImage(my_pic, 0, 0, 200, 200, 20, 20, 200, 200); // Positioning, sizing, clipping } window.onload = draw; </script> </head> <body> <canvas id="my_canvas" width="500" height="350"></canvas> </body> </html>
thumb

JavaScript Videos

Canvas Programming

2D Context Object and Rectangle MethodsFill and Stroke Styles Color Gradient PatternLine Styles for Lines and StrokesBuilding and Drawing PathsDrawing Images and VideosDrawing Text on CanvasRendering Shadows on CanvasCanvas Transformation EffectsPixel Manipulation on CanvasCompositing on the CanvasDynamic Centering and Alignment on the CanvasCanvas Animation and App InitializationObject Oriented Assets OOP on CanvasDetect Mouse Coordinates on CanvasAnimated Background Layers on Canvas TutorialKeyboard Control and MovementJSON Object Arrays and Loops on CanvasHit Detection Mouse Touch Over Object ArraysCollision Detection Hit Game EnemiesParticle Effect System Tutorial Snow Falling AnimationInteractive Canvas Imaging Applications JavaScript PHP TutorialCircular Progress Loader Canvas JavaScript Programming Tutorial