javascript - HTML5 Canvas — Line too thick -
these past 2 days i've been playing around html5 canvas element. i'm attempting draw maze, i'm @ stand-still. line drew isn't consistent linewidth
property. it's ~2px thicker.
i'm familiar half pixel problem canvas element, , need start @ 0.5
, don't know need put 0.5
in code.
if i'm not mistaken, if want make vertical line consistent, x argument needs .5 , horizontal line, y value needs .5?
var canvas = document.getelementbyid("c"), c = canvas.getcontext("2d"), w = canvas.width, h = canvas.height, hallwaywidth = w * 0.10; /*18px*/ c.beginpath(); c.linewidth = 4; c.moveto(0, 0); c.lineto(w / 3, 0); c.moveto(0, 0); c.lineto(0, h); c.moveto(w, 0); c.lineto(w, h); c.moveto(w / 3 + hallwaywidth, 0); c.lineto(w, 0); c.moveto(0, h); c.lineto(w / 2, h); c.moveto(w / 2 + hallwaywidth, h); c.lineto(w, h); /*code thick line*/ c.moveto(hallwaywidth, 0); c.lineto(hallwaywidth, w / 3); c.stroke();
you don't need use 0.5 , when linewidth 4 must use:
2
instead of0
(w-2)
instead ofw
(h-2)
instead ofh
var canvas = document.getelementbyid("c"), c = canvas.getcontext("2d"), w = canvas.width, h = canvas.height, hallwaywidth = w * 0.10, /*18px*/ l = 2; c.beginpath(); c.linewidth = l*2; c.moveto(l,l); c.lineto(w / 3, l); c.moveto(l, l); c.lineto(l, h-l); c.moveto(w-l, l); c.lineto(w-l, h-l); c.moveto(w / 3 + hallwaywidth, l); c.lineto(w-l, l); c.moveto(l, h-l); c.lineto(w / 2, h-l); c.moveto(w / 2 + hallwaywidth, h-l); c.lineto(w-l, h-l); /*code thick line*/ c.moveto(hallwaywidth, l); c.lineto(hallwaywidth, w / 3); c.stroke();
canvas { display: block; margin: 0 auto; margin-top: 50px; /* border: 1px solid red; */ background:red; }
<canvas id="c" width="200" height="200"></canvas>
another option(to avoid "lost pixels" in corners):draw 2 different paths , use "border" linewidth of 8
var canvas = document.getelementbyid("c"), c = canvas.getcontext("2d"), w = canvas.width, h = canvas.height, hallwaywidth = w * 0.10; /*18px*/ c.beginpath(); c.linewidth = 8; c.moveto(0, 0); c.lineto(w / 3, 0); c.moveto(0, 0); c.lineto(0, h); c.moveto(w, 0); c.lineto(w, h); c.moveto(w / 3 + hallwaywidth, 0); c.lineto(w, 0); c.moveto(0, h); c.lineto(w / 2, h); c.moveto(w / 2 + hallwaywidth, h); c.lineto(w, h); c.stroke(); /*code thick line*/ c.beginpath(); c.linewidth = 4; c.moveto(hallwaywidth, 0); c.lineto(hallwaywidth, w / 3); c.stroke();
canvas { display: block; margin: 0 auto; margin-top: 50px; /* border: 1px solid red; */ background: red; }
<canvas id="c" width="200" height="200"></canvas>
Comments
Post a Comment