BitmapData.draw ClipRect 未按预期工作

发布于 2024-11-17 19:41:24 字数 502 浏览 7 评论 0原文

我有一个包含 4 层的组件:(按深度升序)bgImage:Sprite、dropZone:Sprite、dropMask:Sprite 和 line:Sprite。 bgImage位于0,0,其他对象放置在各个正坐标处。 dropMask 正在屏蔽 dropZone。

在位图捕获时,我只想从 dropZone 和 dropMask 图层进行绘制,因此我尝试这样做:

removeChild(bgImage);
removeChild(line);
var bmd:BitmapData = new BitmapData(dropMask.width,dropMask.height,true,0);
bmd.draw(this,null,null,null,dropMask.getBounds(this));

尽管 dropMask 位于 50,60,但捕获始终从 0,0 开始。我也尝试过获取边界矩形并强制 x,y,但这没有什么区别。我在这里做错了什么,我应该忘记clipRect并使用矩阵来代替吗?

I have a component that contains 4 layers: (in ascending depth order) bgImage:Sprite, dropZone:Sprite, dropMask:Sprite and line:Sprite. The bgImage is at 0,0, and the other objects are placed at various positive coordinates. dropMask is masking dropZone.

At the point of bitmap capture I want to only draw from the dropZone and dropMask layers, so I'm trying this:

removeChild(bgImage);
removeChild(line);
var bmd:BitmapData = new BitmapData(dropMask.width,dropMask.height,true,0);
bmd.draw(this,null,null,null,dropMask.getBounds(this));

Despite the dropMask being located at 50,60 the capture always starts from 0,0. I've also tried getting the boundary rectangle and forcing the x,y, but it makes no difference. What am I doing wrong here, and should I just forget clipRect and use a matrix instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疧_╮線 2024-11-24 19:41:24

一个常见的例程:

var rect:Rectangle = dropMask.getRect(dropMask.parent);
var matrix:Matrix = new Matrix();
matrix.translate(-rect.x, -rect.y);
var bmp:BitmapData = new BitmapData(rect.width, rect.height, false, 0xFFFF0000);
bmp.draw(dropMask.parent, matrix);

解决方案步骤:

  1. 在要绘制的坐标空间中获取一个矩形
  2. 获取单位矩阵,并通过 -rectangle.x 转换它,-rectangle.y
  3. draw() 调用中使用该矩阵。

在步骤 1 中,您甚至可能会遇到类似的情况:

import flash.display.Sprite;

var test:Sprite = new Sprite();
test.graphics.beginFill(0, 1);
test.graphics.drawCircle(125, 234, 100);
test.graphics.endFill();

// we are going to draw test, so we get a rectangle 
// in its own coordinate space to deal with registration point offset
var rect:Rectangle = test.getRect(test);
var matrix:Matrix = new Matrix();
matrix.translate(-rect.x, -rect.y);
var bmp:BitmapData = new BitmapData(rect.width, rect.height, false, 0xFFFF0000);
bmp.draw(test, matrix);

// see if we are done
addChild(new Bitmap(bmp));

当我编写使用大量绘图剪辑的代码时,我创建一个矩阵并每次重用它,执行 matrix.identity(); 来重置变换。无需为每个绘图创建新的矩阵。

编辑 不,clipRect 在这里没有帮助。仅当您想要部分绘制某些内容而不是整个剪辑时才使用它。

A common routine for that:

var rect:Rectangle = dropMask.getRect(dropMask.parent);
var matrix:Matrix = new Matrix();
matrix.translate(-rect.x, -rect.y);
var bmp:BitmapData = new BitmapData(rect.width, rect.height, false, 0xFFFF0000);
bmp.draw(dropMask.parent, matrix);

Solution steps:

  1. get a rectangle in coordinate space of what you are going to draw.
  2. get identity matrix, and translate it by -rectangle.x, -rectangle.y
  3. use this matrix in draw() call.

In step 1 you may even encounter something like that:

import flash.display.Sprite;

var test:Sprite = new Sprite();
test.graphics.beginFill(0, 1);
test.graphics.drawCircle(125, 234, 100);
test.graphics.endFill();

// we are going to draw test, so we get a rectangle 
// in its own coordinate space to deal with registration point offset
var rect:Rectangle = test.getRect(test);
var matrix:Matrix = new Matrix();
matrix.translate(-rect.x, -rect.y);
var bmp:BitmapData = new BitmapData(rect.width, rect.height, false, 0xFFFF0000);
bmp.draw(test, matrix);

// see if we are done
addChild(new Bitmap(bmp));

When I code something that uses a lot of drawing clips, I create a matrix and reuse it each time, doing matrix.identity(); to reset transforms. No need to create new matrix for each drawing.

EDIT And no, clipRect doesn't help here. You use it only when you want to draw somthing partially, not the whole clip.

清风夜微凉 2024-11-24 19:41:24

没关系,我误解了clipRect的目的。 http://pixelwelders.com/blog/actionscript-3/2008/ as3-bitmapdata-foibles/.使用矩阵很容易并且可以解决问题。

var mat:Matrix = new Matrix(1,0,0,1,-offsetX,-offsetY);
bmd.draw(this,mat);

Nevermind, I've misunderstood the purpose of clipRect. http://pixelwelders.com/blog/actionscript-3/2008/as3-bitmapdata-foibles/. Using a matrix for this is easy and solves the problem.

var mat:Matrix = new Matrix(1,0,0,1,-offsetX,-offsetY);
bmd.draw(this,mat);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文