哦,我的!伟大的问题!
让我们看看我们有什么。一系列有“约束”的系统,它是2点的集合。 Contraints本身成对出现,并且它们形成两条线,形成一个┘
形状(框的右下角)。
如果我们绘制每个约束线分别,我们会看到这一点:
这是所有水平的红色线和垂直蓝线。我们只画出一个单一的形状┘
,并且每条长长的红色线条确实有数百条小线条,每条线条的底部形状为头尾。这里有几百个┘
,它们一起使它看起来像一个粘合网。事实上,每个人都已经是个人,这使我们很容易。
该形状就是我们需要确定的一种排序边界框。而且它看起来像中的每个Constraint
都带有原始值,因此我们将它们保存为sx
和sy
。
如果我们知道盒子在新位置的边界,并且我们知道原始边界,因为我们已经保存了Contraints的所有Point值,那么我们应该是黄金。
一旦我们有一个约束和其目前的边界框,原来边框为什么,我们所要做的就是调用drawImage方法有两个框:ctx.drawImage(img, sx, sy, sw, sh, dx, dy, dw, dh);
我写了一个新的Constraint.prototype.draw
程序,它看起来像这样:
等。
有几个方法,你可以“打补丁”的孔,它真的给你,否则你必须与转换来欺骗。
看一看代码。我没有太多变化。在代码(我的编辑)和代码DEBUG:
中寻找!!!
(调试代码,以防图像未加载或想要看到线框)。
http://jsfiddle.net/simonsarris/Kuw6P/
的代码是很长,所以我不想贴吧都在这里,但这里的情况下的jsfiddle备份的状态为Down:https://gist.github.com/simonsarris/5405304
这里是最相关的部分:
// !!! new super awesome draw routine! So cool we skipped naming it draw2!
Constraint.prototype.draw3 = function(otherP2) {
// NOW dear friends consider what we have. Each box is made out of two lines,
// the bottom and rightmost ones.
// From these lines we can deduce the topleft and bottom-right points
// From these points we can deduce rectangles
// From the skewed rectangle vs the original rectangle we can "stretch"
// an image, using drawImage's overloaded goodness.
// AND WE'RE OFF:
// destination rect has 2 points:
//top left: Math.min(this.p2.x, otherP2.x), Math.min(this.p2.y, otherP2.y)
//bottom right: (this.p1.x, this.p1.y)
// image destination rectangle, a rect made from the two points
var dx = Math.min(this.p1.x, Math.min(this.p2.x, otherP2.x));
var dy = Math.min(this.p1.y, Math.min(this.p2.y, otherP2.y));
var dw = Math.abs(this.p1.x - Math.min(this.p2.x, otherP2.x));
var dh = Math.abs(this.p1.y - Math.min(this.p2.y, otherP2.y));
// DEBUG: IF THERE IS NO IMAGE TURN THIS ON:
//ctx.strokeStyle = 'lime';
//ctx.strokeRect(dx, dy, dw, dh);
// source rect 2 points:
//top left: Math.min(this.p2.sx, otherP2.sx), Math.min(this.p2.sy, otherP2.sy)
//bottom right: (this.p1.sx, this.p1.sy)
// these do NOT need to be caluclated every time,
// they never change for a given constraint
// calculate them the first time only. I could do this earlier but I'm lazy
// and its past midnight. See also: http://www.youtube.com/watch?v=FwaQxDkpcHY#t=64s
if (this.sx === undefined) {
this.sx = Math.min(this.p1.sx, Math.min(this.p2.sx, otherP2.sx));
this.sy = Math.min(this.p1.sy, Math.min(this.p2.sy, otherP2.sy));
this.sw = Math.abs(this.p1.sx - Math.min(this.p2.sx, otherP2.sx));
this.sh = Math.abs(this.p1.sy - Math.min(this.p2.sy, otherP2.sy));
}
var sx = this.sx;
var sy = this.sy;
var sw = this.sw;
var sh = this.sh;
// DEBUG: IF THERE IS NO IMAGE TURN THIS ON:
//ctx.strokeStyle = 'red';
//ctx.strokeRect(sx, sy, sw, sh);
// IF we have a source and destination rectangle, then we can map an image
// piece using drawImage(img, sx, sy, sw, sh, dx, dy, dw, dh)
// Only problem, we're not exactly dealing with rectangles....
// But we'll deal. Transformations have kooties anyways.
ctx.drawImage(img, sx, sy, sw, sh, dx, dy, dw, dh);
};
不,我不希望任何人调试代码墙我确切的问题是: 我如何映射图像到网格,功能更新(){ var img = new Image(); img.src ='file:/// C:/Users/CL%20Ceintuurbaan/Desktop/texture_2.jpg'; img.onload =函数(){ //创建图案 变种PTRN = ctx.createPattern(IMG, '重复'); \t ctx.clearRect(0,0,canvas.width,canvas.height); \t physics.update(); \t ctx.strokeStyle = ptrn; \t ctx.beginPath(); \t var i = points.length; 012-\t while(i--)points [i] .draw(); \t ctx.stroke(); \t requestAnimFrame(update); }} 是 处理图像渲染的功能。 – vimes1984 2013-03-27 14:24:47
让我编辑问题抱歉的不便。 – vimes1984 2013-03-27 14:26:06
好吧,我明白了。但我担心你必须在墙上的每个单元格上绘制一个“drawImage”。 – 2013-03-27 14:29:02