我在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”?
答案 0 :(得分: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;
};
答案 1 :(得分:-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);
}
我更喜欢第二种解决方案更安全......