IE7 excanvas drawImage workaround?

Ahoy Stack Overflow!

I ran into the following problem: only in IE7, if I call drawImage () after drawing something on the canvas, excanvas does not draw the image with the desired x, y coordinates. I looked at the excanvas / google group project page and found that there were known issues with the drawImage () function. (e.g. http://code.google.com/p/explorercanvas/issues/detail?id=104&q=IE7 )

I tried to restore the identity matrix, as suggested here: The problem with the Excanvas vml position , although it did not seem to have any effect.

Attached is a simple html document demonstrating this problem:

<!DOCTYPE HTML>
<html>
<head>
    <script type="text/javascript" src="excanvas.js"></script>
    <script type="text/javascript">
        window.onload = function(){
            var icon, ctx;

            icon        = new Image();
            icon.width  = "20";
            icon.height = "20";
            icon.src    = "http://www.shangalulu.com/resources/images/icons/small/30.png";
            icon.onload = function(){
                var ctx;

                ctx = document.getElementById('the_canvas').getContext('2d');



                ctx.beginPath();
                ctx.moveTo(0,0);
                ctx.lineTo(500,0);
                ctx.lineTo(500,500);
                ctx.lineTo(0,500);
                ctx.lineTo(0,0);
                ctx.moveTo(200,200);
                ctx.lineTo(300,200);
                ctx.lineTo(300,300);
                ctx.lineTo(200,300);
                ctx.lineTo(200,200);
                ctx.moveTo(0,0);
                ctx.lineTo(500,500);
                ctx.moveTo(0,500);
                ctx.lineTo(500,0);
                ctx.stroke();


                ctx.drawImage(icon, 190, 190, 20, 20);
                ctx.drawImage(icon, 240, 240, 20, 20);
                ctx.drawImage(icon, 290, 290, 20, 20);

                return;
            }
        }
    </script>

</head>
<body>
    <canvas id="the_canvas" width="500" height="500"></canvas>
</body>
</html>

excanvas , : http://code.google.com/p/explorercanvas/

script :

  • ( )
  • 100x100 .
  • , .
  • : , .

: / ?

+3
1

...

, , . , drawImage excanvas.js :

contextPrototype.drawImage = function(image, var_args) {
var dx, dy, dw, dh, sx, sy, sw, sh;

// to find the original width we overide the width and height
var oldRuntimeWidth = image.runtimeStyle.width;
var oldRuntimeHeight = image.runtimeStyle.height;
image.runtimeStyle.width = 'auto';
image.runtimeStyle.height = 'auto';

// get the original size
var w = image.width;
var h = image.height;

// and remove overides
image.runtimeStyle.width = oldRuntimeWidth;
image.runtimeStyle.height = oldRuntimeHeight;

if (arguments.length == 3) {
  dx = arguments[1];
  dy = arguments[2];
  sx = sy = 0;
  sw = dw = w;
  sh = dh = h;
} else if (arguments.length == 5) {
  dx = arguments[1];
  dy = arguments[2];
  dw = arguments[3];
  dh = arguments[4];
  sx = sy = 0;
  sw = w;
  sh = h;
} else if (arguments.length == 9) {
  sx = arguments[1];
  sy = arguments[2];
  sw = arguments[3];
  sh = arguments[4];
  dx = arguments[5];
  dy = arguments[6];
  dw = arguments[7];
  dh = arguments[8];
} else {
  throw Error('Invalid number of arguments');
}
/////////////////////////// MODIFIED BIT ///////////////////////////
var vmlStr = [];
vmlStr.push('<g_vml_:image src="', image.src, '"',
            ' style="position:absolute;',
            ' top:', dy, 'px;',
            ' left:', dx, 'px;',
            ' width:', dw, 'px;',
            ' height:', dh, 'px;"',
            ' cropleft="', sx / w, '"',
            ' croptop="', sy / h, '"',
            ' cropright="', (w - sx - sw) / w, '"',
            ' cropbottom="', (h - sy - sh) / h, '"',
            ' />');
/////////////////////////// END OF MODIFIED BIT ///////////////////////////

this.element_.insertAdjacentHTML('BeforeEnd',
                                vmlStr.join(''));
};

, , VML -, , , . - , . , ...

+1

All Articles