It seems strange, but nevertheless HTML5 supports drawing lines, circles, rectangles and many other basic shapes, it has nothing suitable for drawing a basic point. The only way to do this is to simulate a point with what you have.
So basically there are 3 possible solutions:
- line drawing point
- drawing point as a polygon
- circle drawing point
Each of them has its drawbacks.
Line
function point(x, y, canvas){ canvas.beginPath(); canvas.moveTo(x, y); canvas.lineTo(x+1, y+1); canvas.stroke(); }
Keep in mind that we are reaching the southeast direction, and if this is the edge, a problem may arise. But you can also draw in any other direction.
Rectangle
function point(x, y, canvas){ canvas.strokeRect(x,y,1,1); }
or faster using fillRect, because the render engine will just fill one pixel.
function point(x, y, canvas){ canvas.fillRect(x,y,1,1); }
a circle
One of the problems with circles is that it’s more difficult for the engine to display them.
function point(x, y, canvas){ canvas.beginPath(); canvas.arc(x, y, 1, 0, 2 * Math.PI, true); canvas.stroke(); }
same idea as a rectangle that you can achieve with a fill.
function point(x, y, canvas){ canvas.beginPath(); canvas.arc(x, y, 1, 0, 2 * Math.PI, true); canvas.fill(); }
Problems with all of these solutions:
- It is hard to keep track of all the points that you are about to draw.
- when you zoom in, it looks ugly.
If you are wondering what is the best way to draw a point , I would go with a filled rectangle. You can see jsperf here with benchmarks