-
Notifications
You must be signed in to change notification settings - Fork 2
/
painter.js
153 lines (123 loc) · 4.25 KB
/
painter.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
$(document).ready(function() {
pullParams()
linkify()
})
function pullParams() {
// Pull url paramaters at start
var end = document.location.href.length - 1
var url = document.location.href
// Rid the url of the ending / if ther
if (document.location.href[end] == '/') {
url = url.slice(0,-1)
}
var parse = $.url(url)
var redValue = parse.param('red')
var blueValue = parse.param('blue')
var greenValue = parse.param('green')
// Set default drawing if url is empty
if(!(redValue || greenValue || blueValue)) {
redValue = "(21*sin(x+y)*t/50+(x-y))^(3*t+50)"
greenValue = "(5*sin(x-y)*t/50+(x+y))^t"
blueValue = "23+123*sin(t/20)*sin(t/320*sqrt((123-x)*(123-x) + (45-y)*(45-y)))"
}
// Parse and add if exsists
if(redValue) {
document.getElementById('redFunction').value = redValue;
}
if(blueValue) {
document.getElementById('blueFunction').value = blueValue;
}
if(greenValue) {
document.getElementById('greenFunction').value = greenValue;
}
}
function updateShareURL() {
document.getElementById('url').value = urlize();
}
function draw() {
// Retrieve information from the dom
canvasDraw = document.getElementById('painter');
context = canvasDraw.getContext('2d');
// Retrieve height and width
width = parseInt(canvasDraw.width);
height = parseInt(canvasDraw.height);
// Retrieve functions for drawing each color
redstring = document.getElementById('redFunction').value;
greenstring = document.getElementById('greenFunction').value;
bluestring = document.getElementById('blueFunction').value;
tstate = document.getElementById('tVariable').checked;
tinterval = parseInt(document.getElementById('tInterval').value);
//adjust = document.getElementById('adjust').checked;
imageData = context.createImageData(width, height);
// Start worker for drawing data from function
startDrawWorker(imageData, width, height, redstring, greenstring, bluestring, tinterval, tstate);
// Add stop button if time variable is included
if (tstate) {
document.getElementById('stop').disabled=false;
document.getElementById('start').disabled=true;
}
// Update url for share
updateShareURL()
}
function stopDraw() {
drawWorker.postMessage({'command': 'stop'});
document.getElementById('stop').disabled=true;
document.getElementById('start').disabled=false;
}
function startDrawWorker(imageData, width, height, redstring, greenstring, bluestring, tinterval, tstate) {
//Create Worker
drawWorker = new Worker('paintWorker.js');
//Add Event listener to paste results on the canvas
drawWorker.addEventListener('message', function(e) {handleMessage(e.data)});
//Post initial Message to Worker
drawWorker.postMessage({'command': 'start',
'imageData': imageData,
'width': width,
'height': height,
'redstring': redstring,
'greenstring': greenstring,
'bluestring': bluestring,
'tstate': tstate,
'tinterval': tinterval});
return drawWorker;
}
function handleMessage(data) {
context.putImageData(data.imageData, 0, 0);
}
// Turn current example into a link
function urlize() {
var base = document.location.origin + document.location.pathname
return base + "?" +
$.param({
red: document.getElementById('redFunction').value,
blue: document.getElementById('blueFunction').value,
green: document.getElementById('greenFunction').value
})
}
// Adds links to all examples
function linkify() {
$('.link.single').each(function() {
linkify_single(this)
})
$('.link.multiple').each(function() {
linkify_multiple(this)
})
}
function linkify_single(item) {
newInside = $("<a>", {
href : "./?" + $.param({ red: item.innerText }),
html : item.innerText
})
$(item).html(newInside)
}
function linkify_multiple(item) {
newInside = $("<a>", {
href : "./?" + $.param({
red: $(item).find('.red').text(),
green: $(item).find('.green').text(),
blue: $(item).find('.blue').text()
}),
html : item.innerHTML
})
$(item).html(newInside)
}