-
Contents:
- Description.
- Create a canvas.
- Loading Images Code.
- Drawing Rectangle.
- Clear Rectangle.
- Paths.
- save() & restore().
- Moving Objects in a Canvas.
- Color Objects.
- Draw Text.
- Grayscale Conversion Algorithms.
- Resources.
- A Udacity HTML5 canvas course. Here is its link.
- I will try to make notes on the important things of the course.
- The
<canvas>is just a container for drawing withthe use of JavaScript.
-
HTML Syntax:
<canvas id="canvas" width="500" height="500"></canvas> -
Attributes:
width==> Width of the canvas in CSS pixels (Default 150).height==> Height of the canvas in CSS pixels (Default 150). -
JavaScript Syntax:
const canvas = document.getElementById('canvas');//to select the canvas element const ctx = canvas.getContext('2d');//to get the context of the 2d canvas. It can also be 3d but this course is about 2d.
- In case we want an obaque canvas you can write:
const ctx = canvas.getContext('2d', {alpha: false});
- After setting the context and assign it a variable
ctx. We will use it to draw on the canvas like in the following examples.
-
Using method drawImage()
-
It is called on the CanvasRenderingContext2D interface.
-
Syntax:
It can contain either 3, 5, or 7 parameters:
ctx.drawImage(image, dx, dy);ctx.drawImage(image, dx, dy, dWidth, dHeight);ctx.drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight);Parameters:
image: the image url.dx: the x coordinate of image.dy: the y coordinate of image.sx: The x coordinate where to start clipping.sy: The y coordinate where to start clipping.dWidth: The width to draw the image in the destination canvas. This allows scaling of the drawn image. If not specified, the image is NOT scaled in width when drawn.dHeight: The height to draw the image in the destination canvas. This allows scaling of the drawn image. If not specified, the image is not scaled in height when drawn.sWidth: The width of the clipped image.sHeight: The height of the clipped image.- Example:
<canvas id="canvas" width="300" height="350"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const image = new Image();
image.src = '/*imageUrl*/';
image.onload = function() {
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);//the width and height of the image will be the canvas's width and height.
}
</script> - Syntax:
ctx.fillRect(x,y,width,height);//draws a colored rectangular.ctx.strokeRect(x,y,width,height);//draws an empty rectangular.- Synatx:
ctx.clearRect(x, y, width, height);- This link explains it in details.
save(): [o] saves the most current state to be called after withrestore()[o] If called in the before a current state it saves the default on. An example from MDN(https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/save#Examples). [o] Syntax:
ctx.save();//called after the state was declared.restore(): [o] restores the most recently saved canvas state. [o] Syntax:
ctx.restore();//called before drawing the object we want to restore the state on.ctx.scale(x, y);
ctx.translate(x, y);
ctx.rotate(angleRadians);: anglaeRadian = degrees * (Math.PI/180)
- Order of operations: 1. scale 2. rotate 3. translate
ctx.strokeStyle = "someColor";//color can be a named color (140 named color in HTML), or a hexadecimal color.
ctx.fillStyle = "someColor";
ctx.fill(); //can be used for example if we draw a rectangle by rect() instead of fillRect(), or to color path with the fillStyle color specified.
ctx.strokeText(x, y, [optional max-width]);
ctx.fillText(x, y, [optional max-width]);
-
You can combine both
strokeTextandfillText -
Here is the meme-maker exercise app by udacity.
- https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas
- https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API
- https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage
- https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API
- https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL
- https://www.w3schools.com/tags/canvas_drawimage.asp
- http://diveintohtml5.info/canvas.html#text
- http://html5gamedevelopment.com/2013-12-developing-html5-games-1hr-video-presentation/
- https://www.udemy.com/how-to-program-games/
- https://www.udemy.com/courses/search/?q=html%20canvas&src=ukw&sort=most-reviewed
- A good youtube course.