簡體   English   中英

應用旋轉時,HTML5 畫布剪輯()在 Chrome 中不起作用

[英]HTML5 canvas clip() doesn't work in Chrome when rotation is applied

我正在嘗試在畫布上使用剪輯區域,一旦坐標系被任何非零值旋轉,它就會停止工作:

 window.onload = function() { var canvas = document.getElementById("mainCanvas"); var ctx = canvas.getContext("2d"); ctx.rotate(Math.PI / 8); // !!! Clipping doesn't work with any non zero value ctx.beginPath(); ctx.rect(0, 0, canvas.width, canvas.height); ctx.stroke(); ctx.clip(); // !!! Image below is invisible in Chrome when clip() is enabled var objectImage = document.getElementById("test"); ctx.drawImage(objectImage, 0, 0); }
 <canvas id="mainCanvas" width="320" height="240" style = "border:1px solid #d3d3d3;"></canvas> <img id="test" width="0" height="0" src="https://dl.dropboxusercontent.com/u/4111969/test.png">

該代碼在 Firefox 中運行良好:

Firefox 中的畫布

但在 Chrome 中,矩形內的圖像是空的:

Chrome 中的畫布

平移和縮放轉換似乎工作正常,但不是旋轉。 難道我做錯了什么? 如果這是 Chrome 中的錯誤,是否有好的解決方法?

編輯:

我的系統是:Chrome“Version 49.0.2623.87 m”,Windows 7 Home Premium SP1,ASUS R7 250X 顯卡。 我每次都可以重現這個問題。

我發現如果我在瀏覽器設置中關閉硬件加速,問題就會消失。 據我了解,這意味着我的顯卡驅動程序可能有問題。

有沒有辦法讓我的網頁強制在 Chrome 中進行軟件渲染?

在 Chrome 中,drawImage() 在 Image 加載之前完成,你必須像這樣檢查:

 <!DOCTYPE HTML> <html> <script> window.onload = function() { var canvas = document.getElementById("mainCanvas"); var ctx = canvas.getContext("2d"); ctx.rotate(Math.PI / 8); // !!! Clipping doesn't work with any non zero value ctx.beginPath(); ctx.rect(0, 0, canvas.width, canvas.height); ctx.stroke(); ctx.clip(); //Make sure the Image is loaded var objectImage= new Image(); objectImage.src="https://dl.dropboxusercontent.com/u/4111969/test.png" objectImage.onload =function() { ctx.drawImage(objectImage, 0, 0); } } </script> <body> <img id="test" width="0" height="0" src="https://dl.dropboxusercontent.com/u/4111969/test.png"> <canvas id="mainCanvas" width="320" height="240" style = "border:1px solid #d3d3d3; margin:0; padding:0"></canvas> </body> </html>

問題似乎與瀏覽器硬件加速有關。 一旦我關閉它,一切都正常。

硬件加速瀏覽器設置

但是,我不知道我的網頁是否可以禁用硬件加速。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM