+ 2

How to create a canvas in Html5

I want to know it.

10th Aug 2018, 10:05 AM
Daniel
Daniel - avatar
4 Answers
+ 4
in xxxxx.html (example for canvas 400x400 with black border 1px) <canvas id="canvas" width="400" height="400" style="border:1px solid #000000;"> in xxxxx.js (you need js to draw something) const myCanvas = document.getElementById('canvas'); const ctx = myCanvas.getContext('2d');
10th Aug 2018, 10:34 AM
Dawid
Dawid - avatar
+ 9
use the <canvas></canvas> element inside the body of the html document
10th Aug 2018, 10:17 AM
D_Stark
D_Stark - avatar
+ 3
window.onload = function(){ const myCanvas=document.getElementById("canvas"); const ctx=myCanvas.getContext("2d"); ctx.beginPath(); ctx.arc(100,75,50,0,2*Math.PI); ctx.stroke(); } window.onload - waiting for DOM otherwise can you have error that element returns null ctx.beginPath(); - start drawning ctx.arc(100,75,50,0,2*Math.PI); in example: 100 - x center coordinats 75 - y center coordinats 50 - radius 0 - start (in radians) 2*Math.PI - finish (in radians) 2*Math.PI means full circle ctx.stroke(); - ending draw
10th Aug 2018, 12:12 PM
Dawid
Dawid - avatar
0
Thanks you all! :)
13th Sep 2020, 9:36 AM
Daniel
Daniel - avatar