-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlivefiddle.js
170 lines (151 loc) · 5.58 KB
/
livefiddle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
$(document).ready(function(){
var paused = false;
var ctx = document.getElementById("drawing")
.getContext('2d');
ctx.canvas.width = window.innerWidth;
$(window).resize(function (){
ctx.canvas.width = window.innerWidth;
});
function highlight () {
if (code.getSelection().length > 0 && !isFinite(code.getSelection()) && code.getSelection().indexOf("(") == -1 && eval("typeof(" + code.getSelection() + ") == 'number'")) {
if($("#range").is(":hidden")){
$("#range").show();
$("#varvalue").show();
var current = eval(code.getSelection());
$("#varvalue").val(current);
if (Math.abs(current) < 1) {
$("#range").attr("min",current-1);
$("#range").attr("max",current+1);
} else if (Math.abs(current) < 10) {
$("#range").attr("min",current-5);
$("#range").attr("max",current+5);
} else if (Math.abs(current) < 100) {
$("#range").attr("min",current-50);
$("#range").attr("max",current+50);
}
$("#range").addClass("variable");
$("#range").val(current);
$("#range").css("top", $(".CodeMirror-selected").offset().top + 15);
$("#range").css("left", $(".CodeMirror-selected").offset().left);
$("#varvalue").css("top", $("#range").offset().top);
$("#varvalue").css("left", $("#range").offset().left + $("#range").width() + 10);
}
} else if (code.getSelection().isColor() && $(".miniColors-selector").length == 0 && $("#range").is(":hidden")) {
$(".miniColors-selector").remove();
$("#picker").miniColors("value", code.getSelection());
setTimeout(function(){
$(".miniColors-trigger").click();
$(".miniColors-selector").css("top", $(".CodeMirror-selected").offset().top + 15);
$(".miniColors-selector").css("left", $(".CodeMirror-selected").offset().left);
},10);
} else if (("#" + code.getSelection()).isColor() && $(".miniColors-selector").length == 0 && $("#range").is(":hidden")) {
$(".miniColors-selector").remove();
$("#picker").miniColors("value", "#" + code.getSelection());
setTimeout(function(){
$(".miniColors-trigger").click();
$(".miniColors-selector").css("top", $(".CodeMirror-selected").offset().top + 15);
$(".miniColors-selector").css("left", $(".CodeMirror-selected").offset().left);
},10);
} else if (code.getSelection() != "" && isFinite(code.getSelection())) {
$("#range").removeClass("variable");
if($("#range").is(":hidden")){
$("#range").show();
var current = parseFloat(code.getSelection());
if (Math.abs(current) < 1) {
$("#range").attr("min",current-1);
$("#range").attr("max",current+1);
} else if (Math.abs(current) < 10) {
$("#range").attr("min",current-5);
$("#range").attr("max",current+5);
} else if (Math.abs(current) < 100) {
$("#range").attr("min",current-50);
$("#range").attr("max",current+50);
}
$("#range").val(current);
$("#range").css("top", $(".CodeMirror-selected").offset().top + 15);
$("#range").css("left", $(".CodeMirror-selected").offset().left);
}
} else {
$("#range").hide();
$("#varvalue").hide();
}
};
//var interval = setInterval(tick, 10);
tick();
var code = CodeMirror.fromTextArea(document.getElementById("graph"), { onCursorActivity: highlight});
var lastError = "";
var initExecuted = false;
$("#picker").miniColors({
change: function(hex, rgb) {
if (code.getSelection().indexOf("#") > -1 || (code.getSelection()).isColor() && !("#" + code.getSelection()).isColor()) {
code.replaceSelection(hex);
} else {
code.replaceSelection(hex.replace("#",""));
}
}
});
$("#load").change(function(){
code.setValue($("#" + $(this).val()).val());
$("#restart").click();
ctx = document.getElementById("drawing")
.getContext('2d');
});
$("#range").change(function(){
var rounded = Math.round(parseFloat($(this).val())*10000)/10000;
if ($(this).hasClass("variable")) {
eval(code.getSelection() + "=" + rounded + ";");
$("#varvalue").val(rounded);
} else {
code.replaceSelection("" + rounded);
}
});
$("#restart").click(function() {
eval(code.getValue().match(/function init(.*[\s\S].*)*}/g)[0]);
if (typeof(init) == "function") {
init();
initExecuted = true;
}
});
$("#pause").click(function() {
if ($(this).hasClass("paused")){
paused = false;
$(this).attr("id","pause");
$(this).html("Pause");
} else {
paused = true;
$(this).attr("id","play");
$(this).html("Play");
}
$(this).toggleClass("paused");
});
function tick() {
try {
if (initExecuted == false) {
eval(code.getValue().match(/function init(.*[\s\S].*)*}/g)[0]);
init();
initExecuted = true;
}
if (!paused) {
ctx.restore();
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
if ($("#varvalue").is(":visible")) {
$("#varvalue").val(eval(code.getSelection()));
}
eval(code.getValue());
}
} catch(err) {
if (err != lastError) {
console.log(err);
lastError = err;
}
}
requestAnimationFrame(tick);
}
String.prototype.isColor = function () {
var test = $("#test").css("color", this);
if (test.attr("style") && (this.length > 6 && this.match("#") || !this.match("#")))
return true;
else
return false;
};
});