我正在尝试使用以下代码根据设备的方向在移动浏览器上旋转画布
e=window.event;
var w=window.innerWidth||window.screen.availWidth;
var h=window.innerHeight||window.screen.availHeight;
var ww = colouringImage.width;
var hh = colouringImage.height;
var c = document.createElement('canvas');
var cntx = c.getContext('2d');
if(this.canvas.height > h) {
c.setAttribute('width', hh);
c.setAttribute('height',ww);
cntx.rotate(-90 * Math.PI / 180);
cntx.drawImage(colouringImage, -ww, 0);
this.artboard.canvas.width = hh;
this.artboard.canvas.height = ww;
this.artboard.clearRect(0,0,hh,ww);
this.artboard.drawImage(c,0,0);
c = document.createElement('canvas');
c.setAttribute('width', hh);
c.setAttribute('height',ww);
cntx = c.getContext('2d');
cntx.rotate(-90 * Math.PI / 180);
cntx.drawImage(this.canvas,-ww,0);
this.canvas.width = hh;
this.canvas.height = ww;
this.context.clearRect(0,0,hh,ww);
this.context.drawImage(c,0,0);
}else if (this.canvas.width > w)
{
c.setAttribute('width', ww);
c.setAttribute('height', hh);
cntx.drawImage(colouringImage, 0, 0);
this.artboard.canvas.width= ww;
this.artboard.canvas.height = hh;
this.artboard.clearRect(0,0,ww,hh);
this.artboard.drawImage(c,0,0);
c = document.createElement('canvas');
c.setAttribute('width',ww);
c.setAttribute('height',hh);
cntx = c.getContext('2d');
cntx.rotate(90 * Math.PI / 180);
cntx.drawImage(this.canvas,0,-ww);
cntx.translate(hh/2,ww/2)
cntx.rotate(0 * Math.PI / 180);
this.canvas.width = ww;
this.canvas.height=hh;
this.context.clearRect(0,0,ww,hh);
this.context.drawImage(c,0,0);
}
我在这里所做的是复制已经绘制的内容并将其旋转到临时画布上,然后在清除后将其绘制回画布上。但是,正在发生的事情是它似乎没有清除图像。图像是 png 并且需要是透明的。
还有其他方法可以清除画在画布上的图像吗?