OK, I got it. There were a few problems. First of all, before the image is cached, it wouldn't even load without error on the line:
Code:
newdiv.getContext("2d").drawImage(i,0,0);
This problem would be more dramatic on a live page. Also, I just didn't like the idea of drawing to the canvas before it was appended to the page, so I switched things around there and made the initial drawing contingent upon the load event of the image. There were a number of undeclared variables, I declared them.
Now this business of:
Code:
tempimg.src = curcanvas.toDataURL();
In testing here, the code never executed that line, there was always an error.
I think it's just wrong. The src of an image object is an image file. Here there is either a data mismatch (toDataURL of the canvas tag is not the same as toDataURL of an actual image would be, and perhaps is null) or you are feeding it the raw image data when it expects a file containing the image data, or more likely there is a security violation. Further, since all you are doing is rotating the canvas, there is no need to use data for the image updated from the last rotation. The very same image originally used will suffice, and guess what? - It's already loaded. This allows a number of steps to be skipped.
This can probably be simplified further, and definitely be written so as to not expose so many global variables, as well as to use valid HTML code, but it works:
Code:
<html>
<head>
<title>Web Site Thing</title>
<script type="text/javascript">
function firefoxrotate(rotnum) {
var curcanvas = document.getElementById(curmover);
var curcanvasctx = curcanvas.getContext("2d");
curcanvasctx.clearRect(0, 0, curcanvas.width, curcanvas.height);
curcanvasctx.translate(100, 100);
curcanvasctx.rotate(rotnum/180 * Math.PI);
curcanvasctx.translate(-100, -100);
curcanvasctx.drawImage(i,0,0);
}
</script>
</head>
<body id="mybody">
<script type="text/javascript">
var ni = document.getElementById('mybody');
var divIdName = "img1";
var i = new Image();
var newdiv = document.createElement('canvas');
newdiv.setAttribute('id',divIdName);
newdiv.setAttribute('width', '200');
newdiv.setAttribute('height', '200');
var ctx = newdiv.getContext("2d");
ni.appendChild(newdiv);
i.onload = function(){ctx.drawImage(i,0,0);};
i.src = "yourimage.png";
var curmover="img1";
</script>
<div align=right>
<button onclick="firefoxrotate(-15)">FF Rot-15</button>
<button onclick="firefoxrotate(15)">FF Rot15</button>
<button onclick="firefoxrotate(45)">FF Rot45</button>
<button onclick="firefoxrotate(135)">FF Rot135</button>
</div>
</body>
</html>
Bookmarks