2

Javascriptでは、送信する前に座標平面を回転および変換してグラフィックをレンダリングすることがよくあります。

ctx.save();
ctx.translate(someX, someY);
ctx.rotate(someAngle * Math.PI / 180);

ctx.beginPath();
ctx.moveTo(x1, y1);    // What's the actual (x1,y1)?
ctx.lineTo(x2, y2);    // What's the actual (x2,y2)?
ctx.stroke();

ctx.restore();

これを行った後、描画した線分の端点の実際の値をどのように把握しますか?その後、平行移動と回転を行うと、(x1、y1)と(x2、y2)は、平行移動と回転がない場合の位置から遠く離れているためです。それらの実際の値が何であるかを知る簡単な方法はありますか?

4

2 に答える 2

3

現在の変換行列を取得する方法は現在ないため、回転/平行移動/スケーリングを自分で追跡する必要があります。

実際に変換を実行するには、変換行列に点を(列ベクトルとして)乗算する必要があります。

変換に影響を与えるメソッドをオーバーライドして、マトリックスの独自のコピーを格納できます。私はこのコードをテストしていませんが、次のようなものが機能するはずです:

var contextPrototype = CanvasRenderingContext2D.prototype;

contextPrototype.xform = Matrix.I(3);

contextPrototype.realSave = contextPrototype.save;
contextPrototype.save = function() {
    if (!this.xformStack) {
        this.xformStack = [];
    }
    this.xformStack.push(this.xform.dup());
    this.realSave();
}

contextPrototype.realRestore = contextPrototype.restore;
contextPrototype.restore = function() {
    if (this.xformStack && this.xformStack.length > 0) {
        this.xform = this.xformStack.pop();
    }
    this.realRestore();
}

contextPrototype.realScale = contextPrototype.scale;
contextPrototype.scale = function(x, y) {
    this.xform = this.xform.multiply($M([
        [x, 0, 0],
        [0, y, 0],
        [0, 0, 1]
    ]));
    this.realScale(x, y);
}

contextPrototype.realRotate = contextPrototype.rotate;
contextPrototype.rotate = function(angle) {
    var sin = Math.sin(angle);
    var cos = Math.cos(angle);
    this.xform = this.xform.multiply($M([
        [cos, -sin, 0],
        [sin,  cos, 0],
        [   0,   0, 1]
    ]));
    this.realRotate(angle);
}

contextPrototype.realTranslate = contextPrototype.translate;
contextPrototype.translate = function(x, y) {
    this.xform = this.xform.multiply($M([
        [1, 0, x],
        [0, 1, y],
        [0, 0, 1]
    ]));
    this.realTranslate(x, y);
}

contextPrototype.realTransform = contextPrototype.transform;
contextPrototype.transform = function(m11, m12, m21, m22, dx, dy) {
    this.xform = this.xform.multiply($M([
        [m11, m21, dx],
        [m12, m22, dy],
        [  0,   0,  1]
    ]));
    this.realTransform(m11, m12, m21, m22, dx, dy);
}

contextPrototype.realSetTransform = contextPrototype.setTransform;
contextPrototype.setTransform = function(m11, m12, m21, m22, dx, dy) {
    this.xform = $M([
        [m11, m21, dx],
        [m12, m22, dy],
        [  0,   0,  1]
    ]);
    this.realSetTransform(m11, m12, m21, m22, dx, dy);
}

便宜上シルベスター行列ライブラリを使用しましたが、独自の乗算を行うこともできます。

変換されたポイントを取得するには、変換行列にポイントを掛けるだけです。

// Get the transformed point as [x, y]
contextPrototype.getTransformedPoint = function(x, y) {
    var point = this.xform.multiply($V([x, y, 1]));
    return [point.e(1), point.e(2)];
}
于 2009-05-11T22:45:21.167 に答える
1

それを見つける唯一の方法は、実際の座標を知りたいポイントに、レンダリングコンテキストで行ったのと同じ変換を適用することだと思います。いくつかのライブラリは、次のような行列演算を提供します。http: //sylvester.jcoglan.com/デカルト座標 への回転演算を実行してみることができます。

于 2009-05-11T20:03:00.153 に答える