erratic-gate/scripts/light.js

85 lines
1.8 KiB
JavaScript
Raw Normal View History

console.log("bulb");
function light(id){
this.id = id;
var g = document.createElementNS('http://www.w3.org/2000/svg', 'g');
$(g).attr({
width: "100%",
height: "100%"
});
var el = $(document.createElementNS('http://www.w3.org/2000/svg', 'circle')).attr({
cx: "50",
cy:"50",
r: "40",
id: this.id,
fill:"white",
stroke: "black"
});
$(el).attr("stroke-width","2");
$(el).attr("class","light");
$(g).append(el);
//$(el).attr("onmousedown","selected='"+this.id+"';");
$(el).attr("onmouseup","selected='yay';");
var elem = document.getElementById("svg1");
elem.appendChild(g);
this.rep = el;
this.i = new pin(0);
this.val = false;
//addevt(this);
$((this.rep)).attr("stroke-width",4);
this.activation = function(){
return this.i.val;
}
this.x = function(){
let name = "#" + this.id;
return parseFloat($(name).attr("cx"));
}
this.y = function(){
let name = "#" + this.id;
//console.log("y"+parseFloat($(name).attr("y")));
return parseFloat($(name).attr("cy"));
}
//design
this.init = function(){
return 0;
}
this.update = function(){
if (this.i.val != "yay"){
if ((this.activation())){
$((this.rep)).attr("fill","yellow");
}
else{
$((this.rep)).attr("fill","white");
}
}
//console.log($((this.rep)).attr("fill"));
let x = this.x();
let y = this.y();
this.i.set(x-60,y-10);
}
pieces[pieces.length] = this;
addclk_light(this);
}
function addclk_light(ob){
var rep = ob.rep;
$(ob.rep).on("mousedown touchstart",function(e){
e.preventDefault();
var svg = document.getElementById("svg1");
$(svg).append(ob.rep);
$(svg).append($(ob.i.rep));
selected=ob.id;
});
}