HTML5 canvas clip() doesn#39;t work in Chrome when rotation is applied(应用旋转时,HTML5 画布剪辑()在 Chrome 中不起作用)
问题描述
我正在尝试在画布上使用剪辑区域,一旦坐标系旋转任何非零值,它就会停止工作:
I'm trying to use a clip region on a canvas and it stops working as soon as the coordinate system is rotated by any non zero value:
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 中运行良好:
The code works fine in Firefox:
但在 Chrome 中,矩形内的图像是空的:
But in Chrome the image inside the rectangle is empty:
平移和缩放转换似乎可以正常工作,但旋转却不行.难道我做错了什么?如果这是 Chrome 中的错误,有没有好的解决方法?
Translate and scale transformations seem to work fine, but not the rotate. Am I doing something wrong? If it's a bug in Chrome, is there a good workaround?
我的系统是:Chrome版本 49.0.2623.87 m",Windows 7 Home Premium SP1,ASUS R7 250X 显卡.我每次都能重现这个问题.
My system is: Chrome "Version 49.0.2623.87 m", Windows 7 Home Premium SP1, ASUS R7 250X graphics card. I can reproduce the problem every time.
我发现如果我在浏览器设置中关闭硬件加速,问题就会消失.据我了解,这意味着我的显卡驱动程序可能存在问题.
I found that the problem goes away if I turn off hardware acceleration in the browser settings. As I understand this means there's probably a problem with my graphics card driver.
有没有办法让我的网页强制在 Chrome 中呈现软件?
Is there a way for my webpage to force the software rendering in Chrome?
推荐答案
问题似乎与浏览器硬件加速有关.关闭后一切正常.
The problem seems to be related to the browser hardware acceleration. Everything works fine as soon as I turn it off.
但是,我不知道我的网页是否可以禁用硬件加速.
However, I don't know if it's possible for my web page to disable hardware acceleration.
这篇关于应用旋转时,HTML5 画布剪辑()在 Chrome 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:应用旋转时,HTML5 画布剪辑()在 Chrome 中不起作用
基础教程推荐
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01