我可以重复使用 html5 canvas 元素的转换吗?
想象一下,我想要转换画布上下文坐标空间以包含某个边界框,并且我正在围绕它编写测试。
是否有可能实际“使用”上下文的转换,有点像这样:
function toBoundingBox( context, upleft, botright ) {
// ...
}
// and the test function:
function test( canvaselement ) {
var canvasbox = {
topleft: {x:0, y:0},
botright: {x:canvaselement.width, y:canvaselement.height} };
var ctx = canvaselement.getContext("2d");
toBoundingBox( ctx, {x:-1,y:-1}, {x:2, y: -5} );
var thetransform = ctx.getTransform();
assert( thetransform( {x:-1,y:-1} ) == canvasbox.topleft );
assert( thetransform( {x:2, y:-5} ) == canvasbox.botright );
}
或者还有其他方法来编写这个测试函数吗?
Imagine I want to transform a canvas context coordinate space to contain a certain bounding box, and that I'm writing a test around it.
Would it be possible to actually 'use' the context's transformation, somewhat like this:
function toBoundingBox( context, upleft, botright ) {
// ...
}
// and the test function:
function test( canvaselement ) {
var canvasbox = {
topleft: {x:0, y:0},
botright: {x:canvaselement.width, y:canvaselement.height} };
var ctx = canvaselement.getContext("2d");
toBoundingBox( ctx, {x:-1,y:-1}, {x:2, y: -5} );
var thetransform = ctx.getTransform();
assert( thetransform( {x:-1,y:-1} ) == canvasbox.topleft );
assert( thetransform( {x:2, y:-5} ) == canvasbox.botright );
}
Or is there any other way to write this test function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
遗憾的是,没有本地方法来访问转换。您需要自己实现这一点。有关详细信息,请参阅 HTML5 Canvas 获取变换矩阵?。希望这有帮助!
Sadly there's no native way to access transformation. You'll need to implement this yourself. See HTML5 Canvas get transform matrix? for further info. Hopefully this helps!