質問

は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)とした後、遠く離れた彼らは、並進と回転せずになるだろうどこからどこかにあるため。彼らの実際の値が何であるかを見つけるための簡単な方法はありますか?

役に立ちましたか?

解決

は、現在の変換行列を取得するために今方法はありませんので、あなた自身をスケーリング/任意の回転/翻訳を追跡する必要があります。

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

あなたは行列のあなた自身のコピーを格納するための変換に影響を与える方法をオーバーライドすることができます。私はこのコードをテストしていませんが、このような作業をする必要があります:

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)];
}

他のヒント

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

への回転操作を実行しようとすることができます
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top