OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 var editor; | 5 function load(source) { |
| 6 var editorWindow = |
| 7 document.getElementsByClassName('editor-frame')[0].contentWindow; |
6 | 8 |
7 function createEditor() { | 9 // ImageEditor makes extensive use of Function.prototype.bind. |
8 editor = new ImageEditor(document.getElementById('frame'), save, close); | 10 // Add it if it does not exist (as in Safari 5). |
9 window.addEventListener('resize', resize); | 11 if (!editorWindow.Function.prototype.bind) { |
10 load(createTestGrid()); | 12 editorWindow.Function.prototype.bind = bindToObject; |
| 13 } |
| 14 |
| 15 editorWindow.ImageUtil.trace.bindToDOM( |
| 16 document.getElementsByClassName('debug-output')[0]); |
| 17 |
| 18 editorWindow.ImageEditor.open(save, close, source); |
11 } | 19 } |
12 | 20 |
13 function load(source) { | 21 function save(blob) { |
14 editor.onModeCancel(); | 22 console.log('Saving ' + blob.size + ' bytes'); |
15 editor.getBuffer().load(source); | |
16 } | |
17 | |
18 function save(canvas) { | |
19 var blob = ImageEncoder.getBlob(canvas, 'image/jpeg'); | |
20 console.log('Blob size: ' + blob.size + ' bytes'); | |
21 } | 23 } |
22 | 24 |
23 function close() { | 25 function close() { |
24 document.body.innerHTML = 'Editor closed, hit reload'; | 26 document.body.innerHTML = 'Editor closed, hit reload'; |
25 } | 27 } |
26 | 28 |
27 function resize() { | |
28 var wrapper = document.getElementsByClassName('canvas-wrapper')[0]; | |
29 editor.getBuffer().resizeScreen( | |
30 wrapper.clientWidth, wrapper.clientHeight, true); | |
31 } | |
32 | |
33 | |
34 function getUrlField() { | 29 function getUrlField() { |
35 return document.getElementById('imageUrl'); | 30 return document.getElementsByClassName('image-url')[0]; |
36 } | 31 } |
37 | 32 |
38 function createTestGrid() { | 33 function createTestGrid() { |
39 var canvas = document.createElement('canvas'); | 34 var canvas = document.createElement('canvas'); |
40 canvas.width = 1000; | 35 canvas.width = 1000; |
41 canvas.height = 1000; | 36 canvas.height = 1000; |
42 | 37 |
43 var context = canvas.getContext('2d'); | 38 var context = canvas.getContext('2d'); |
44 | 39 |
45 var imageData = context.getImageData(0, 0, canvas.width, canvas.height); | 40 var imageData = context.getImageData(0, 0, canvas.width, canvas.height); |
46 fillGradient(imageData); | 41 fillGradient(imageData); |
47 context.putImageData(imageData, 0, 0); | 42 context.putImageData(imageData, 0, 0); |
48 | 43 |
49 drawTestGrid(context); | 44 drawTestGrid(context); |
50 | 45 |
51 return canvas; | 46 return canvas; |
52 } | 47 } |
53 | 48 |
54 function fillGradient(imageData) { | 49 function fillGradient(imageData) { |
55 var data = imageData.data; | 50 var data = imageData.data; |
56 var width = imageData.width; | 51 var width = imageData.width; |
57 var height = imageData.height; | 52 var height = imageData.height; |
58 | 53 |
59 var maxX = width - 1; | 54 var maxX = width - 1; |
60 var maxY = height - 1; | 55 var maxY = height - 1; |
61 var maxDist = maxX + maxY; | 56 var maxDist = maxX + maxY; |
62 var values = ImageUtil.precomputeByteFunction( function(dist) { | 57 var values = []; |
63 return Math.round(dist/maxDist*255); | 58 for (var i = 0; i <= maxDist; i++) { |
64 }, maxDist); | 59 values.push(Math.max(0, Math.min(0xFF, Math.round(i/maxDist*255)))); |
| 60 } |
65 | 61 |
66 var index = 0; | 62 var index = 0; |
67 for (var y = 0; y != height; y++) | 63 for (var y = 0; y != height; y++) |
68 for (var x = 0; x != width; x++) { | 64 for (var x = 0; x != width; x++) { |
69 data[index++] = values[maxX - x + maxY - y]; | 65 data[index++] = values[maxX - x + maxY - y]; |
70 data[index++] = values[x + maxY - y]; | 66 data[index++] = values[x + maxY - y]; |
71 data[index++] = values[maxX - x + y]; | 67 data[index++] = values[maxX - x + y]; |
72 data[index++] = 0xFF; | 68 data[index++] = 0xFF; |
73 } | 69 } |
74 } | 70 } |
75 | 71 |
76 function drawTestGrid(context) { | 72 function drawTestGrid(context) { |
77 var width = context.canvas.width; | 73 var width = context.canvas.width; |
78 var height = context.canvas.height; | 74 var height = context.canvas.height; |
79 | 75 |
80 context.textBaseline = 'top'; | 76 context.textBaseline = 'top'; |
81 | 77 |
82 const STEP = 100; | 78 const STEP = 100; |
83 for (var y = 0; y < height; y+= STEP) { | 79 for (var y = 0; y < height; y+= STEP) { |
84 for (var x = 0; x < width; x+= STEP) { | 80 for (var x = 0; x < width; x+= STEP) { |
85 context.strokeRect(x + 0.5, y + 0.5, STEP, STEP); | 81 context.strokeRect(x + 0.5, y + 0.5, STEP, STEP); |
86 context.strokeText(x + ',' + y, x + 2, y); | 82 context.strokeText(x + ',' + y, x + 2, y); |
87 } | 83 } |
88 } | 84 } |
89 } | 85 } |
90 | 86 |
91 // It is nice to be able to test on Safari. | 87 function bindToObject(thisObject) { |
92 // Safari 5 does not have Function.bind, so we are adding that just in case. | |
93 Function.prototype.bind = function(thisObject) { | |
94 var func = this; | 88 var func = this; |
95 var args = Array.prototype.slice.call(arguments, 1); | 89 var args = Array.prototype.slice.call(arguments, 1); |
96 function bound() { | 90 function bound() { |
97 return func.apply( | 91 return func.apply( |
98 thisObject, args.concat(Array.prototype.slice.call(arguments, 0))); | 92 thisObject, args.concat(Array.prototype.slice.call(arguments, 0))); |
99 } | 93 } |
100 bound.toString = function() { return "bound: " + func; }; | 94 bound.toString = function() { return "bound: " + func; }; |
101 return bound; | 95 return bound; |
102 }; | 96 }; |
103 | 97 |
OLD | NEW |