DEV Community

Lam
Lam

Posted on

14 13

HTML Canvas cheat sheet

Certainly! Here's an HTML Canvas cheat sheet that covers some of the commonly used methods and properties:

Canvas Setup:

<!-- Create a canvas element -->
<canvas id="myCanvas"></canvas>

<!-- Get the canvas element in JavaScript -->
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
Enter fullscreen mode Exit fullscreen mode

Drawing Shapes:

// Draw a rectangle
ctx.beginPath();
ctx.rect(x, y, width, height);
ctx.fillStyle = "color";
ctx.fill();
ctx.closePath();

// Draw a circle
ctx.beginPath();
ctx.arc(x, y, radius, startAngle, endAngle, anticlockwise);
ctx.fillStyle = "color";
ctx.fill();
ctx.closePath();

// Draw a line
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.strokeStyle = "color";
ctx.stroke();
ctx.closePath();
Enter fullscreen mode Exit fullscreen mode

Text Manipulation:

ctx.font = "fontStyle fontSize fontFamily";
ctx.fillStyle = "color";
ctx.fillText("Text", x, y);
ctx.strokeText("Text", x, y);
Enter fullscreen mode Exit fullscreen mode

Colors and Styles:

// Set fill color
ctx.fillStyle = "color";

// Set stroke color
ctx.strokeStyle = "color";

// Set line width
ctx.lineWidth = width;

// Set line join style
ctx.lineJoin = "style";

// Set line cap style
ctx.lineCap = "style";

// Set shadow
ctx.shadowColor = "color";
ctx.shadowBlur = value;
ctx.shadowOffsetX = value;
ctx.shadowOffsetY = value;
Enter fullscreen mode Exit fullscreen mode

Transformations:

// Translate the canvas origin
ctx.translate(x, y);

// Scale the canvas
ctx.scale(scaleX, scaleY);

// Rotate the canvas
ctx.rotate(angle);

// Reset transformations
ctx.setTransform(1, 0, 0, 1, 0, 0);
Enter fullscreen mode Exit fullscreen mode

Image Manipulation:

// Draw an image
var img = new Image();
img.src = "imageURL";
img.onload = function() {
    ctx.drawImage(img, x, y, width, height);
}

// Draw a portion of an image
ctx.drawImage(img, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight);

// Create a pattern with an image
var pattern = ctx.createPattern(image, "repeat/repeat-x/repeat-y/no-repeat");
ctx.fillStyle = pattern;
ctx.fill();
Enter fullscreen mode Exit fullscreen mode

Check demo simplepixelart.com

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (1)

Collapse
 
terranrich profile image
Richard Brum

Certainly! I can write a comment that's written entirely by AI. In a typical comment, one will either praise or denigrate the article in question. In this case, both are deserved: praise for the usefulness of this article; and scorn for the use of AI to write it. Programmers typically have enough expertise to write an article like this without the use of AI; however, some will lazily accept it as an alternative to actual work.

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more