HTML5 Canvas gradient appears black -
i'm trying reproduce following background image in html5 canvas, renders on browser black box. original image made in illustrator , that's i'm getting coordinates (actually used export plug-in).
<html lang="en"> <head> <meta charset="utf-8" /> <title>index</title> <script> function init() { var canvas = document.getelementbyid("canvas"); var ctx = canvas.getcontext("2d"); draw(ctx); } function draw(ctx) { var gradient; // layer1/red background bottom ctx.save(); ctx.beginpath(); ctx.moveto(1036.0, 789.7); ctx.lineto(12.0, 789.7); ctx.lineto(12.0, 21.7); ctx.lineto(1036.0, 21.7); ctx.lineto(1036.0, 789.7); ctx.closepath(); ctx.fillstyle = "rgb(235, 10, 10)"; ctx.fill(); // layer1/shadow bottom ctx.beginpath(); ctx.moveto(1036.0, 788.7); ctx.lineto(12.0, 788.7); ctx.lineto(12.0, 20.7); ctx.lineto(1036.0, 20.7); ctx.lineto(1036.0, 788.7); ctx.closepath(); ctx.save(); ctx.transform(3.200, 0.000, 0.000, -4.206, 196.6, -5329.4); gradient = ctx.createradialgradient(96.7, -1580.0, 0.0, 96.7, -1580.0, 161.7); gradient.addcolorstop(0.00, "rgb(0, 0, 0)"); gradient.addcolorstop(0.55, "rgb(0, 0, 0)"); gradient.addcolorstop(1.00, "rgb(0, 0, 0)"); ctx.fillstyle = gradient; ctx.fill(); ctx.restore(); ctx.restore(); } </script> </head> <body onload="init()"> <canvas id="canvas" width="1049" height="792"></canvas> </body> </html>
edit: realized forgot add alphas in gradient stops, change below solves problem:
gradient.addcolorstop(0.00, "rgba(0, 0, 0,0)"); gradient.addcolorstop(1.00, "rgba(0, 0, 0, 0.5)");
the main problem gradient stops..
gradient.addcolorstop(0.00, "rgb(0, 0, 0)"); gradient.addcolorstop(0.55, "rgb(0, 0, 0)"); gradient.addcolorstop(1.00, "rgb(0, 0, 0)");
they black (red 0, green 0, blue 0). changing values ...
gradient.addcolorstop(0.00, "rgb(255, 0, 0)"); gradient.addcolorstop(0.55, "rgb(125, 0, 0)"); gradient.addcolorstop(1.00, "rgb(0, 0, 0)");
gives full red in centre (0) (red 255, green 0, blue 0) half red in middle (.55) (red 125, green 0, blue 0) , no red (black) @ edge (1.00) (red 0, green 0, blue 0)
then have change centre point of gradient.
function init() { var canvas = document.getelementbyid("canvas"); var ctx = canvas.getcontext("2d"); draw(ctx); } function draw(ctx) { var gradient; // layer1/red background bottom ctx.save(); ctx.beginpath(); ctx.moveto(1036.0, 789.7); ctx.lineto(12.0, 789.7); ctx.lineto(12.0, 21.7); ctx.lineto(1036.0, 21.7); ctx.lineto(1036.0, 789.7); ctx.closepath(); ctx.fillstyle = "rgb(235, 10, 10)"; ctx.fill(); // layer1/shadow bottom ctx.beginpath(); ctx.moveto(1036.0, 788.7); ctx.lineto(12.0, 788.7); ctx.lineto(12.0, 20.7); ctx.lineto(1036.0, 20.7); ctx.lineto(1036.0, 788.7); ctx.closepath(); ctx.save(); ctx.transform(3.200, 0.000, 0.000, -4.206, 196.6, -5329.4); gradient = ctx.createradialgradient(96.7, -1380.0, 0.0, 96.7, -1580.0, 261.7); gradient.addcolorstop(0.00, "rgb(250, 0, 0)"); gradient.addcolorstop(0.55, "rgb(125, 0, 0)"); gradient.addcolorstop(1.00, "rgb(50, 0, 0)"); ctx.fillstyle = gradient; ctx.fill(); ctx.restore(); ctx.restore(); }
<body onload="init()"> <canvas id="canvas" width="1049" height="792"></canvas> </body>
Comments
Post a Comment