1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
9 [[15.2,32.1,7.6],[3,0.329,0.15,0.321,0.145,0.709,3,2,4,0.269,0.662],[0,"#000000",3,"#f5f5c1",12,"#158a34",68,"#89e681",100]],
10 [[15.2,32.1,5],[3,0.273,0.117,0.288,0.243,0.348,3,2,4,0.269,0.662],[1,"#000000",3,"#f5f5c1",8,"#158a34",17,"#89e681",20]],
11 [[4,12,1],[2,0.115,0.269,0.523,0.34,0.746,3,4,4,0.028,0.147],[0,"#36065e",0,"#c24242",77,"#8a19b0",91,"#ff9900",99,"#f5c816",99]],
12 [[4,12,1],[3,0.12,0.218,0.267,0.365,0.445,3,4,4,0.028,0.147],[0,"#000000",0,"#0f8a84",38,"#f5f5c1",43,"#158a34",70,"#89e681",100]],
13 [[4,12,1],[0,0.09,0.276,0.27,0.365,0.445,1,4,4,0.028,0.147],[0,"#93afd9",11,"#9cf0ff",92,"#edfdff",100]],
14 [[10.4,12,1],[2,0.082,0.302,0.481,0.35,0.749,2,3,4,0.028,0.147],[0,"#000000",11,"#ffffff",22,"#19a68a",85,"#6b0808",98]],
15 [[7.8,27.2,2.6],[3,0.21,0.714,0.056,0.175,0.838,2,0,2,0.132,0.311],[0,"#0a1340",0,"#ffffff",55,"#4da8a3",83,"#2652ab",99,"#2f1e75",46]],
16 [[4,12,1],[2,0.115,0.269,0.496,0.34,0.767,3,4,4,0.028,0.147],[0,"#b8cfcf",0,"#3f5a5c",77,"#1a330a",91,"#c0e0dc",99]],
17 [[10.6,31.8,1],[1,0.157,0.092,0.256,0.098,0.607,3,4,4,0.015,0.34],[0,"#4d3e3e",0,"#9a1ac9",77,"#aaf09e",100]],
21 * A helper function to abbreviate getElementById.
23 * @param {string} elementId The id to get.
26 function $(elementId) {
27 return document.getElementById(elementId);
33 * @return {string} MIME type
35 function PNaClmimeType() {
36 return 'application/x-pnacl';
40 * Check if the browser supports PNaCl.
44 function browserSupportsPNaCl() {
45 var mimetype = PNaClmimeType();
46 return navigator.mimeTypes[mimetype] !== undefined;
50 * Get the URL for Google Cloud Storage.
52 * @param {string} name The relative path to the file.
55 function getDataURL(name) {
56 var revision = 231964;
57 var baseUrl = 'http://commondatastorage.googleapis.com/gonacl/demos/publish/';
58 return baseUrl + revision + '/smoothlife/' + name;
62 * Create the Native Client <embed> element as a child of the DOM element
65 * @param {string} name The name of the example.
66 * @param {number} width The width to create the plugin.
67 * @param {number} height The height to create the plugin.
68 * @param {Object} attrs Dictionary of attributes to set on the module.
70 function createNaClModule(name, width, height, attrs) {
71 var moduleEl = document.createElement('embed');
72 moduleEl.setAttribute('name', 'nacl_module');
73 moduleEl.setAttribute('id', 'nacl_module');
74 moduleEl.setAttribute('width', width);
75 moduleEl.setAttribute('height', height);
76 moduleEl.setAttribute('path', '');
77 moduleEl.setAttribute('src', getDataURL(name + '.nmf'));
78 moduleEl.setAttribute('type', PNaClmimeType());
80 // Add any optional arguments
82 for (var key in attrs) {
83 moduleEl.setAttribute(key, attrs[key]);
87 // The <EMBED> element is wrapped inside a <DIV>, which has both a 'load'
88 // and a 'message' event listener attached. This wrapping method is used
89 // instead of attaching the event listeners directly to the <EMBED> element
90 // to ensure that the listeners are active before the NaCl module 'load'
92 var listenerDiv = $('listener');
93 listenerDiv.appendChild(moduleEl);
97 * Add the default event listeners to the element with id "listener".
99 function attachDefaultListeners() {
100 var listenerDiv = $('listener');
101 listenerDiv.addEventListener('load', moduleDidLoad, true);
102 listenerDiv.addEventListener('error', moduleLoadError, true);
103 listenerDiv.addEventListener('progress', moduleLoadProgress, true);
104 listenerDiv.addEventListener('message', handleMessage, true);
105 listenerDiv.addEventListener('crash', handleCrash, true);
110 * Called when the Browser can not communicate with the Module
112 * This event listener is registered in attachDefaultListeners above.
114 * @param {Object} event
116 function handleCrash(event) {
117 if (naclModule.exitStatus == -1) {
118 updateStatus('CRASHED');
120 updateStatus('EXITED [' + naclModule.exitStatus + ']');
125 * Called when the NaCl module is loaded.
127 * This event listener is registered in attachDefaultListeners above.
129 function moduleDidLoad() {
130 var bar = $('progress');
133 naclModule = $('nacl_module');
139 * Hide the status field and progress bar.
141 function hideStatus() {
142 $('statusField').style.display = 'none';
143 $('progress').style.display = 'none';
147 * Called when the plugin fails to load.
149 * @param {Object} event
151 function moduleLoadError(event) {
152 updateStatus('Load failed.');
156 * Called when the plugin reports progress events.
158 * @param {Object} event
160 function moduleLoadProgress(event) {
161 $('progress').style.display = 'block';
163 var loadPercent = 0.0;
164 var bar = $('progress');
166 if (event.lengthComputable && event.total > 0) {
167 loadPercent = event.loaded / event.total * 100.0;
169 // The total length is not yet known.
172 bar.value = loadPercent;
176 * If the element with id 'statusField' exists, then set its HTML to the status
179 * @param {string} opt_message The message to set.
181 function updateStatus(opt_message) {
182 var statusField = $('statusField');
184 statusField.style.display = 'block';
185 statusField.textContent = opt_message;
190 * Add event listeners after the NaCl module has loaded. These listeners will
191 * forward messages to the NaCl module via postMessage()
193 function attachListeners() {
194 $('preset').addEventListener('change', loadSelectedPreset);
195 $('reset').addEventListener('click', loadSelectedPreset);
196 $('clear').addEventListener('click', function() { clear(0); });
197 $('splat').addEventListener('click', function() { splat(); });
198 $('brushSizeRange').addEventListener('change', function() {
199 var radius = parseFloat(this.value);
200 setBrushSize(radius, 1.0);
201 $('brushSize').textContent = radius.toFixed(1);
205 function loadSelectedPreset() {
206 loadPreset($('preset').value);
209 function loadPreset(index) {
210 var preset = presets[index];
213 setKernel.apply(null, preset[0]);
214 setSmoother.apply(null, preset[1]);
215 setPalette.apply(null, preset[2]);
219 function clear(color) {
220 naclModule.postMessage({cmd: 'clear', color: color});
223 function setKernel(discRadius, ringRadius, blendRadius) {
224 naclModule.postMessage({
226 discRadius: discRadius,
227 ringRadius: ringRadius,
228 blendRadius: blendRadius});
231 function setSmoother(type, dt, b1, d1, b2, d2, mode, sigmoid, mix, sn, sm) {
232 naclModule.postMessage({
235 b1: b1, d1: d1, b2: b2, d2: d2,
236 mode: mode, sigmoid: sigmoid, mix: mix,
240 function setPalette() {
241 var repeating = arguments[0] !== 0;
244 for (var i = 1; i < arguments.length; i += 2) {
245 colors.push(arguments[i]);
246 stops.push(arguments[i + 1]);
248 naclModule.postMessage({
250 repeating: repeating,
256 naclModule.postMessage({cmd: 'splat'});
259 function setBrushSize(radius, color) {
260 naclModule.postMessage({cmd: 'setBrush', radius: radius, color: color});
265 * Handle a message coming from the NaCl module.
266 * @param {Object} message_event
268 function handleMessage(message_event) {
270 $('fps').textContent = message_event.data.toFixed(1);
274 * Listen for the DOM content to be loaded. This event is fired when parsing of
275 * the page's document has finished.
277 document.addEventListener('DOMContentLoaded', function() {
278 updateStatus('Loading...');
279 if (!browserSupportsPNaCl()) {
280 updateStatus('Browser does not support PNaCl or PNaCl is disabled');
281 } else if (naclModule == null) {
282 createNaClModule('smoothnacl', 512, 512);
283 attachDefaultListeners();
285 // It's possible that the Native Client module onload event fired
286 // before the page's onload event. In this case, the status message
287 // will reflect 'SUCCESS', but won't be displayed. This call will
288 // display the current message.
289 updateStatus('Waiting.');