javascript - Image not loading on Canvas? -
i'm trying load image onto canvas , hasn't been working after trying everything...
javascript:
var canvas = document.getelementbyid('mycanvas'); var context = canvas.getcontext('2d'); var start = new image(); start.onload = function(){ context.drawimage(start, 0, 0, start.width, start.height, 0, 0, canvas.width, canvas.height); } start.src = "hangman0.png";
i don't see wrong code, trying draw image , scale canvas' width , height, picture isn't showing no matter what. pointers? i've tried changing both html5 , css code, still no avail.
css:
canvas{ background-color: rgb(0,198,255); border-style: ridge; border-width: 5px; border-color: rgb(157,255,0); position: relative; }
html5:
<body> <div id='section'> <canvas id='mycanvas' width='979px' height='560px'></canvas> <script src="projectjavascript.js"></script> </div> </body>
thanks in advance help!
your offered code works me long use window.onload
:
are wrapping script
code inside window.onload
?
<script> window.onload=(function(){ var canvas = document.getelementbyid('mycanvas'); var context = canvas.getcontext('2d'); var start = new image(); start.onload = function(){ context.drawimage(start, 0, 0, start.width, start.height, 0, 0, canvas.width, canvas.height); } start.src = "https://dl.dropboxusercontent.com/u/139992952/multple/rainy.png"; }); // end window.onload </script>
example code , demo:
note: stacksnippets automatically wrap js-script code inside window.onload
var canvas = document.getelementbyid('mycanvas'); var context = canvas.getcontext('2d'); var start = new image(); start.onload = function(){ context.drawimage(start, 0, 0, start.width, start.height, 0, 0, canvas.width, canvas.height); } start.src = "https://dl.dropboxusercontent.com/u/139992952/multple/rainy.png";
body{ background-color: ivory; } canvas{border:1px solid red; margin:0 auto; }
<canvas id='mycanvas' width='979px' height='560px'></canvas>
Comments
Post a Comment