forked from hydra-synth/hydra
-
Notifications
You must be signed in to change notification settings - Fork 6
/
gallery.js
286 lines (261 loc) · 8.85 KB
/
gallery.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
const request = require('superagent')
const examples = require('./examples.json')
const sketches = []
class Gallery {
constructor (callback) {
this.sketches = []
this.examples = []
this.current = null
this.code = null
this.exampleIndex = null
// request.get('/sketches').end((err, res) => {
// console.log('got sketches', res.text, err)
// if(err) {
// console.log('err getting sketches', err)
// } else {
// this.sketches = JSON.parse(res.text)
// }
this.examples = examples
this.setSketchFromURL(callback)
// callback(this.code, this.foundSketch)
// })
window.addEventListener('popstate', (event) => {
this.setSketchFromURL(callback)
// console.log("location: " + document.location + ", state: " + JSON.stringify(event.state));
});
this.setRandomSketch = this.setRandomSketch.bind(this)
}
clear() {
this.current = null
this.code = null
//this.exampleIndex = null
let newurl = window.location.protocol + '//' + window.location.host + window.location.pathname
window.history.pushState({ path: newurl }, '', newurl)
this.url = newurl
}
setSketchFromURL(callback) {
hush()
render(o0)
let searchParams = new URLSearchParams(window.location.search)
let base64Code = searchParams.get('code')
// if(!base64Code) base64Code = searchParams.get('id') // backwards compatibility with earlier form of naming. id is now called code
let sketch_id = searchParams.get('sketch_id')
let code = ''
//console.log("id", sketch_id, "code", base64Code)
// boolean to determine whether a sketch was found based on the URL, either through looking through the database or rendering the code
this.foundSketch = false
// if contains a sketch id, set sketch from id
if(sketch_id) {
var sketch = this.getExampleById(sketch_id)
if(sketch) {
// console.log('found', sketch)
this.setSketch(sketch)
callback(this.code, false)
} else {
request
.get('/sketchById')
.query({sketch_id: sketch_id})
.end((err, res) => {
// console.log('got sketches', res.text, err)
if(err) {
console.log('err getting sketches', err)
this.setSketchFromCode(base64Code, callback)
} else {
this.sketches = JSON.parse(res.text)
if(this.sketches.length > 0) {
this.setSketch(this.sketches[0])
//this.code = this.decodeBase64(this.sketches[0].code)
this.foundSketch = true
callback(this.code, this.foundSketch)
} else {
this.setSketchFromCode(base64Code, callback)
}
}
})
}
} else {
this.setSketchFromCode(base64Code, callback)
}
//
// // console.log('found ', sketch)
// // if(sketch) {
// // this.setSketch(sketch)
// // this. = true0
// // } else if (base64Code){
// // this.code = this.decodeBase64(base64Code)
// // this.foundSketch = true
// // } else {
// // console.log('id not found', sketch_id)
// // this.setRandomSketch()
// // }
//
// // // backwards combaitbility with earlier shareable URLS
// } else {
//
// if (base64Code) {
// this.code = this.decodeBase64(base64Code)
// this.foundSketch = true
// } else {
// this.setRandomSketch()
// }
// }
}
setSketchFromCode(base64Code, callback){
if (base64Code) {
this.code = this.decodeBase64(base64Code)
this.foundSketch = true
} else {
this.setRandomSketch()
}
callback(this.code, this.foundSketch)
}
saveImage() {
}
setToURL(params){
// console.log(base64)
// console.log('params', params)
// keep code in url for backwards compatibility / compatibility between local and public versions
var url_params
if(params.sketch_id) {
url_params = `sketch_id=${params.sketch_id}`
} else {
url_params = `sketch_id=${params.sketch_id}&code=${params.code}`
}
// } else {
// url_params = params.map( (param, index) => `${param.label}=${param.value}`).join('&')
// }
// console.log('url params', url_params)
let newurl = window.location.protocol + '//' +
window.location.host + window.location.pathname + '?' + url_params
// window.location.href = newurl
window.history.replaceState({ path: newurl }, '', newurl)
this.url = newurl
}
encodeBase64(text) {
return btoa(encodeURIComponent(text))
}
decodeBase64(base64Code) {
return decodeURIComponent(atob(base64Code))
}
setSketch(sketch) {
this.code = this.decodeBase64(sketch.code)
this.current = sketch
// this.setToURL('sketch_id', sketch._id)
// let params = Object.keys(sketch).map( (key) => {
// return {label: key, value: sketch[key]}
// })
this.setToURL(sketch)
}
setRandomSketch() {
// if there are sketches, set code from sketch, otherwise generate random
//console.log("examples length", this.examples, this.exampleIndex)
if(this.examples.length > 0) {
let index
// if(this.exampleIndex === null) {
index = Math.floor(Math.random() * this.examples.length)
while (index === this.exampleIndex) {
index = Math.floor(Math.random() * this.examples.length)
}
// } else {
// index = this.exampleIndex +1
// if(index >= this.examples.length) index = 0
// }
this.exampleIndex = index
// console.log('example is', this.examples[rand])
this.setSketch(this.examples[index])
} else {
var startString = 'osc(' + 2 + Math.floor(Math.pow(10, Math.random() * 2)) + ')'
startString += '.color(' + Math.random().toFixed(2) + ',' + Math.random().toFixed(2) + ',' + Math.random().toFixed(2)+ ')'
startString += '.rotate(' + Math.random().toFixed(2) + ')'
startString += '.out(o0)'
this.code = startString
}
}
// shares via twitter
shareSketch(code, hydra, name) {
this.saveSketch(code, () => {
console.log("URL is", this.url, 'sketch is', this.current)
hydra.getScreenImage((img) => {
request
.post('/image')
.attach('previewImage', img)
.query({
url: this.url,
sketch_id: this.current.sketch_id,
name: name
})
// .send({
// code: base64
// })
// .query(query)
.end((err, res) => {
if(err) {
console.log('error postingimage', err)
} else {
console.log('image response', res.text)
// self.setToURL([ { label: 'sketch_id', value: res.text}, {label: 'code', value: base64} ])
}
})
// var oReq = new XMLHttpRequest();
// oReq.open("POST", "https://localhost:8000/image", true);
// oReq.onload = function (oEvent) {
// // Uploaded.
// console.log("uploaded", oEvent)
// };
// oReq.send(img);
// console.log('got image', img)
})
})
}
saveSketch(code, callback) {
let self = this
//console.log('saving in gallery', code)
let base64 = this.encodeBase64(code)
// console.log('code is', base64)
let query = {
code: base64,
parent: this.current ? this.current.sketch_id : null
}
console.log('saving in gallery', query)
request
.post('/sketch')
// .send({
// code: base64
// })
.query(query)
.end((err, res) => {
if(err) {
console.log('error posting sketch', err)
if(callback) callback(err)
} else {
console.log('response', res.text)
// self.setToURL([ { label: 'sketch_id', value: res.text}, {label: 'code', value: base64} ])
self.setSketch({
sketch_id: res.text,
code: base64
})
if(callback) callback(null)
}
})
}
saveLocally(code) {
let base64 = this.encodeBase64(code)
// keep code in url for backwards compatibility / compatibility between local and public versions
var url_params = `code=${base64}`
// } else {
// url_params = params.map( (param, index) => `${param.label}=${param.value}`).join('&')
// }
//console.log('url params', url_params)
let newurl = window.location.protocol + '//' +
window.location.host + window.location.pathname + '?' + url_params
window.history.pushState({ path: newurl }, '', newurl)
this.url = newurl
}
getExampleById(id) {
//console.log('looking for', id, this.examples, this.sketches)
var sketches = this.examples.filter((sketch) => sketch.sketch_id === id)
if(sketches.length <= 0) sketches = this.sketches.filter((sketch) => sketch.sketch_id === id)
return sketches[0]
}
}
module.exports = Gallery