0

私は、mudcu の sketch.js のスクリプトを使用して、単純なペイント アプリケーションを作成しました。

コード内でいくつかの変更を加えましたが、鉛筆の色を設定する関数をトリガーするのに問題があります。

var Sketch = function(config) { "use strict";
var that = this;
...........
var ctx2 = layer2d[2];
// Style object.
this.zoom = 1;

this.style = {
    tool: "brush",
    globalAlpha: 1,
    globalCompositeOperation: "source-over",
    strokeStyle: "#000",
    lineWidth: 5,
    lineCap: "round",
    lineJoin: "round"

私の質問は、ユーザーがボタンをクリックしたときに、「this.style」の strokeStyle をたとえば「#D55151」に設定するにはどうすればよいですか?

4

2 に答える 2

0

JQuery を使用することは、問題の良い解決策です。

<button id="myBtn">My Button</button>

JQuery

$('#myBtn').click(function(){
  // set you style
  that.style.strokeStyle = "#D55151"; // var that = this;
});

アップデート:

純粋なJavaScriptの答え:

document.getElementById('myBtn').onclick=function(){
   // set you style
  that.style.strokeStyle = "#D55151"; // var that = this;
};
于 2013-07-18T09:52:06.690 に答える
-1

あなたは付け加えられます:

function(config, style) {
    this.style = style || this.style = {
            tool: "brush",
            globalAlpha: 1,
            globalCompositeOperation: "source-over",
            strokeStyle: "#000",
            lineWidth: 5,
            lineCap: "round",
            lineJoin: "round"
        };
}

そしてそれを次のように呼び出します

$("#somebutton").on("click", function() {
    Sketch({config}, {overides style});
});

または、jQuery またはアンダースコアを使用している場合はさらに良い:

function(config, style) {
    this.style = $.extend({
            tool: "brush",
            globalAlpha: 1,
            globalCompositeOperation: "source-over",
            strokeStyle: "#000",
            lineWidth: 5,
            lineCap: "round",
            lineJoin: "round"
        }, style);
}

私は2番目の解決策をより安全に好む...

于 2013-07-18T10:01:31.453 に答える