| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 function load(source) { | |
| 6 var editorWindow = | |
| 7 document.getElementsByClassName('editor-frame')[0].contentWindow; | |
| 8 | |
| 9 // ImageEditor makes extensive use of Function.prototype.bind. | |
| 10 // Add it if it does not exist (as in Safari 5). | |
| 11 if (!editorWindow.Function.prototype.bind) { | |
| 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); | |
| 19 } | |
| 20 | |
| 21 function save(blob) { | |
| 22 console.log('Saving ' + blob.size + ' bytes'); | |
| 23 } | |
| 24 | |
| 25 function close() { | |
| 26 document.body.innerHTML = 'Editor closed, hit reload'; | |
| 27 } | |
| 28 | |
| 29 function getUrlField() { | |
| 30 return document.getElementsByClassName('image-url')[0]; | |
| 31 } | |
| 32 | |
| 33 function createTestGrid() { | |
| 34 var canvas = document.createElement('canvas'); | |
| 35 canvas.width = 1000; | |
| 36 canvas.height = 1000; | |
| 37 | |
| 38 var context = canvas.getContext('2d'); | |
| 39 | |
| 40 var imageData = context.getImageData(0, 0, canvas.width, canvas.height); | |
| 41 fillGradient(imageData); | |
| 42 context.putImageData(imageData, 0, 0); | |
| 43 | |
| 44 drawTestGrid(context); | |
| 45 | |
| 46 return canvas; | |
| 47 } | |
| 48 | |
| 49 function fillGradient(imageData) { | |
| 50 var data = imageData.data; | |
| 51 var width = imageData.width; | |
| 52 var height = imageData.height; | |
| 53 | |
| 54 var maxX = width - 1; | |
| 55 var maxY = height - 1; | |
| 56 var maxDist = maxX + maxY; | |
| 57 var values = []; | |
| 58 for (var i = 0; i <= maxDist; i++) { | |
| 59 values.push(Math.max(0, Math.min(0xFF, Math.round(i/maxDist*255)))); | |
| 60 } | |
| 61 | |
| 62 var index = 0; | |
| 63 for (var y = 0; y != height; y++) | |
| 64 for (var x = 0; x != width; x++) { | |
| 65 data[index++] = values[maxX - x + maxY - y]; | |
| 66 data[index++] = values[x + maxY - y]; | |
| 67 data[index++] = values[maxX - x + y]; | |
| 68 data[index++] = 0xFF; | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 function drawTestGrid(context) { | |
| 73 var width = context.canvas.width; | |
| 74 var height = context.canvas.height; | |
| 75 | |
| 76 context.textBaseline = 'top'; | |
| 77 | |
| 78 const STEP = 100; | |
| 79 for (var y = 0; y < height; y+= STEP) { | |
| 80 for (var x = 0; x < width; x+= STEP) { | |
| 81 context.strokeRect(x + 0.5, y + 0.5, STEP, STEP); | |
| 82 context.strokeText(x + ',' + y, x + 2, y); | |
| 83 } | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 function bindToObject(thisObject) { | |
| 88 var func = this; | |
| 89 var args = Array.prototype.slice.call(arguments, 1); | |
| 90 function bound() { | |
| 91 return func.apply( | |
| 92 thisObject, args.concat(Array.prototype.slice.call(arguments, 0))); | |
| 93 } | |
| 94 bound.toString = function() { return "bound: " + func; }; | |
| 95 return bound; | |
| 96 }; | |
| 97 | |
| OLD | NEW |