这是它的要点:我有一个 getColor 方法和一个 setColor 方法。
- getColor 更新指向
color_obj
当前对象的全局指针。 - setColor 使用该指针来更改对象的颜色。
问题是,它只将其绘制为黑色。
这是小提琴:http: //jsfiddle.net/EbvH7/3/
和来源 - html:
<html>
<!--- Click the square box to get its color.
Then change the color and hit set.
For some reason it draws the color black only.... -->
<head>
<script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/v4.2.0/kinetic-v4.2.0.min.js"></script>
<script src="http://jscolor.com/jscolor/jscolor.js"></script>
<script>
var stage; //canvas
var layer_frame = new Kinetic.Layer(); //Blue frame holder
var color_obj; //global pointer to object to be colored.
function getColor(obj) {
color_obj = obj; //set global pointer
var fill = '"'+color_obj.getFill()+'"';
var name = color_obj.getName();
document.getElementById('color_pick').color.fromString(fill);
document.getElementById('colorname_obj').innerHTML = name;
}
function setColor(){
//Use global pointer
var fill = document.getElementById('color_pick').color;
var name = document.getElementById('colorname_obj').innerHTML;
color_obj.setFill('"'+fill+'"');
color_obj.setName(name);
stage.draw()
}
function setup() {
stage = new Kinetic.Stage({
container: 'container',
width: 400, height: 400
});
var frame_area = new Kinetic.Rect({
x: 100, y: 100, opacity: 1,
width: 30, height: 30,
fill: 'a7cccb', stroke: 'black', strokeWidth: 2,
name: 'blue_box'
});
frame_area.on('click', function() {
getColor(this)
});
// add the layer to the stage
layer_frame.add(frame_area);
stage.add(layer_frame);
stage.draw();
};
</script>
</head>
<body onload="setup()">
<!--Canvas--->
<div id="container"></div>
<!--Panels-->
<div id="panel_left">
<!--- C O L O R B O X -->
<div id="colorbox">
<h7 id="colorname_obj" >None</h7><br/>
<input class="color" id="color_pick" style="width:50%" value="000000" >
<input type="button" id="color_set" value="Set" onclick="setColor()" >
</div>
</div>
</body>
</html>
和CSS:
#panel_left {
position:absolute;
top:30px; left:30px;
width:100px; background-color:red;
}
#container {
position:absolute;
top:30px; left:130px;
background-color:blue;
width:50%; height:50%;
}