Hosting Solutions
mobile_logo Table of Contents
seal_frontseal_back

2D Context Object and Rectangle Methods

Welcome to the first exercise of the Canvas Bootcamp. If you are new to canvas element programming, this initial exercise is where you can begin your training. We use JavaScript to deliver all control and assets to our canvas. In this first exercise we will cover the three immediate rectangle methods and discuss the 2D context object and referring back to its corresponding canvas element. <!DOCTYPE html> <html> <head> <style> body{ margin:40px; background:#666; } #my_canvas{ background:#FFF; border:#000 1px solid; } </style> <script> function draw() { var ctx = document.getElementById("my_canvas").getContext("2d"); // You can back-reference the canvas element through the context object alert(ctx.canvas.id+" | "+ctx.canvas.width+" | "+ctx.canvas.height); ctx.fillRect(20,20,100,100); ctx.fillRect(200,200,50,50); ctx.strokeRect(20,20,100,100); ctx.clearRect(0, 0, 100, 100); } window.onload = draw; </script> </head> <body> <canvas id="my_canvas" width="400" height="300"></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