-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathpainter.js
More file actions
167 lines (157 loc) · 6.09 KB
/
Copy pathpainter.js
File metadata and controls
167 lines (157 loc) · 6.09 KB
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
/* global AFRAME Blob uploadcare */
var saveAs = require('../../vendor/saveas.js').saveAs;
AFRAME.registerSystem('painter', {
init: function () {
this.brushSystem = this.sceneEl.systems.brush;
this.showTemplateItems = true;
function getUrlParams () {
var match;
var pl = /\+/g; // Regex for replacing addition symbol with a space
var search = /([^&=]+)=?([^&]*)/g;
var decode = function (s) { return decodeURIComponent(s.replace(pl, ' ')); };
var query = window.location.search.substring(1);
var urlParams = {};
match = search.exec(query);
while (match) {
urlParams[decode(match[1])] = decode(match[2]);
match = search.exec(query);
}
return urlParams;
}
var urlParams = getUrlParams();
if (urlParams.url || urlParams.urljson) {
var isBinary = urlParams.urljson === undefined;
this.brushSystem.loadFromUrl(urlParams.url || urlParams.urljson, isBinary);
document.getElementById('logo').setAttribute('visible', false);
document.getElementById('acamera').setAttribute('orbit-controls', 'position', '0 1.6 3');
document.getElementById('apainter-logo').classList.remove('hidden');
//document.getElementById('apainter-author').classList.remove('hidden'); // not used yet
}
if (urlParams.bgcolor !== undefined) {
document.body.style.backgroundColor = '#' + urlParams.bgcolor;
}
if (urlParams.sky !== undefined) {
this.sceneEl.addEventListener('loaded', function (evt) {
if (urlParams.sky === '') {
document.getElementById('sky').setAttribute('visible', false);
} else {
document.getElementById('sky').setAttribute('material', 'src', 'url(' + urlParams.sky + ')');
}
});
}
if (urlParams.floor !== undefined) {
this.sceneEl.addEventListener('loaded', function (evt) {
if (urlParams.floor === '') {
document.getElementById('ground').setAttribute('visible', false);
} else {
document.getElementById('ground').setAttribute('material', 'src', 'url(' + urlParams.floor + ')');
}
});
}
this.startPainting = false;
var self = this;
document.addEventListener('stroke-started', function (event) {
if (!self.startPainting) {
document.getElementById('logo').emit('fadeout');
self.startPainting = true;
}
});
// @fixme This is just for debug until we'll get some UI
document.addEventListener('keyup', function (event) {
if (event.keyCode === 8) {
// Undo (Backspace)
self.brushSystem.undo();
}
if (event.keyCode === 67) {
// Clear (c)
self.brushSystem.clear();
}
if (event.keyCode === 78) {
// Next brush (n)
var hands = document.querySelectorAll('[paint-controls]');
var brushesNames = Object.keys(AFRAME.BRUSHES);
var index = brushesNames.indexOf(hands[0].components.brush.data.brush);
index = (index + 1) % brushesNames.length;
[].forEach.call(hands, function (hand) {
hand.setAttribute('brush', 'brush', brushesNames[index]);
});
}
if (event.keyCode === 82) {
// Random stroke (r)
self.brushSystem.generateRandomStrokes(1);
}
if (event.keyCode === 76) {
// load binary from file (l)
self.brushSystem.loadFromUrl('demo.apa', true);
}
if (event.keyCode === 85) { // u - upload
self.upload();
}
if (event.keyCode === 86) { // v - save
self.save();
}
if (event.keyCode === 74) { // j - save json
self.saveJSON();
}
if (event.keyCode === 79) { // o - toggle template objects+images visibility
self.showTemplateItems = !self.showTemplateItems;
var templateItems = document.querySelectorAll('.templateitem');
for (var i = 0; i < templateItems.length; i++) {
templateItems[i].setAttribute('visible', self.showTemplateItems);
}
}
});
console.info('A-PAINTER Version: ' + this.version);
},
saveJSON: function () {
var json = this.brushSystem.getJSON();
var blob = new Blob([JSON.stringify(json)], {type: 'application/json'});
saveAs(blob, 'demo.json');
},
save: function () {
var dataviews = this.brushSystem.getBinary();
var blob = new Blob(dataviews, {type: 'application/octet-binary'});
saveAs(blob, 'demo.apa');
},
upload: function (success, error) {
this.sceneEl.emit('drawing-upload-started');
var self = this;
var baseUrl = 'https://aframe.io/a-painter/?url=';
var dataviews = this.brushSystem.getBinary();
var blob = new Blob(dataviews, {type: 'application/octet-binary'});
var uploader = 'uploadcare'; // or 'fileio'
if (uploader === 'fileio') {
// Using file.io
var fd = new window.FormData();
fd.append('file', blob);
var xhr = new window.XMLHttpRequest();
xhr.open('POST', 'https://file.io'); // ?expires=1y
xhr.onreadystatechange = function (data) {
if (xhr.readyState === 4) {
var response = JSON.parse(xhr.response);
if (response.success) {
console.log('Uploaded link: ', baseUrl + response.link);
self.sceneEl.emit('drawing-upload-completed', {url: baseUrl + response.link});
if (success) { success(); }
}
} else {
self.sceneEl.emit('drawing-upload-error', {errorInfo: null, fileInfo: null});
if (error) { error(); }
}
};
xhr.send(fd);
} else {
var file = uploadcare.fileFrom('object', blob);
file.done(function (fileInfo) {
console.log('Uploaded link: ', baseUrl + fileInfo.cdnUrl);
self.sceneEl.emit('drawing-upload-completed', {url: baseUrl + fileInfo.cdnUrl});
if (success) { success(); }
}).fail(function (errorInfo, fileInfo) {
self.sceneEl.emit('drawing-upload-error', {errorInfo: errorInfo, fileInfo: fileInfo});
if (error) { error(errorInfo); }
}).progress(function (uploadInfo) {
self.sceneEl.emit('drawing-upload-progress', {progress: uploadInfo.progress});
});
}
}
});