html5 - Drawing shape on canvas with relative size -
i have code draws circle. how change code red circle 100% of browser window? want red circle resize browser window.
<canvas width="100%" height="100%"></canvas> var ctx; function draw() { ctx = $('canvas').get(0).getcontext('2d'); ctx.canvas.width = window.innerwidth; ctx.canvas.height = window.innerheight; } function circle(x, y, r, c) { ctx.beginpath(); var rad = ctx.createradialgradient(x, y, 1, x, y, r); rad.addcolorstop(0, 'rgba('+c+',1)'); rad.addcolorstop(1, 'rgba('+c+',0)'); ctx.fillstyle = rad; ctx.arc(x, y, r, 0, math.pi*2, false); ctx.fill(); } draw(); circle(128, 128, 200, '255,0,0');
consider jsfiddle
on load
/resize
:
create circle draw()
setvars()
circle(...)
draw()
(which sets width/height of canvas) clear canvas (see: how clear canvas redrawing)
var ctx, canvas, x, y, w, h, r; function draw() { ctx = $('canvas').get(0).getcontext('2d'); canvas = ctx.canvas; canvas.width = window.innerwidth; canvas.height = window.innerheight; } function setvars() { w = canvas.width; h = canvas.height; x = w/2; y = h/2; r = x < y ? x : y; } function circle(x, y, r, c) { ctx.beginpath(); var rad = ctx.createradialgradient(x, y, 1, x, y, r); rad.addcolorstop(0, 'rgba(' + c + ',1)'); rad.addcolorstop(1, 'rgba(' + c + ',0)'); ctx.fillstyle = rad; ctx.arc(x, y, r, 0, math.pi * 2, false); ctx.fill(); } function makecircle() { draw(); setvars(); circle(x, y, r, '255,0,0'); } $(window).resize(function () { // redraw (onresize) makecircle(); }); // draw (onload) makecircle();
Comments
Post a Comment