HTML5 Canvas / Context操作

时间:2019-01-17 19:50:58

标签: html5-canvas

我有一个包含图像的Canvas元素。现在我想将画布旋转90度。我已经开始工作了。现在,我想通过canvas.toDataURL();

提取旋转后的图像

但是,我对DataURL()的调用仍然返回“预旋转”图像。我究竟做错了什么?我在屏幕上看到了重新绘制的旋转图像,但在调用toDataURL()时却看不到。

 var canvas = document.querySelector('#ID_REVIEW_DIV canvas');
        var context = canvas.getContext("2d");
        var cw = canvas.width;
        var ch = canvas.height;

        if (skew == 90) {

                 var myImage;
                    // store current data to an image
                    myImage = new Image();
                    myImage.src = canvas.toDataURL('image/jpeg', 0.7);

                    myImage.onload = function () {
                        // reset the canvas with new dimensions
                        canvas.width = ch;
                        canvas.height = cw;
                        cw = canvas.width;
                        ch = canvas.height;

                        context.save();
                        context.translate(cw, ch / cw);
                        context.rotate(90 * Math.PI / 180);                       
                        context.drawImage(myImage, 0, 0);                          
                        context.restore();
                        //Should be the updated rotated image now...
                        dataString = canvas.toDataURL('image/jpeg', 0.7);

                        // clear the temporary image
                        myImage = null;

                    }
        }

        if (dataString != null) {
            dataString = document.querySelector('#ID_REVIEW_DIV canvas').toDataURL('image/jpeg', 0.7);
        }

0 个答案:

没有答案