scratch – Blame information for rev 108

Subversion Repositories:
Rev:
Rev Author Line No. Line
84 office 1 window.DrawingBoard = typeof DrawingBoard !== "undefined" ? DrawingBoard : {};
2  
3  
4 DrawingBoard.Utils = {};
5  
6 /*!
7 * Tim (lite)
8 * github.com/premasagar/tim
9 *//*
10 A tiny, secure JavaScript micro-templating script.
11 */
12 DrawingBoard.Utils.tpl = (function(){
13 "use strict";
14  
15 var start = "{{",
16 end = "}}",
17 path = "[a-z0-9_][\\.a-z0-9_]*", // e.g. config.person.name
18 pattern = new RegExp(start + "\\s*("+ path +")\\s*" + end, "gi"),
19 undef;
20  
21 return function(template, data){
22 // Merge data into the template string
23 return template.replace(pattern, function(tag, token){
24 var path = token.split("."),
25 len = path.length,
26 lookup = data,
27 i = 0;
28  
29 for (; i < len; i++){
30 lookup = lookup[path[i]];
31  
32 // Property not found
33 if (lookup === undef){
34 throw "tim: '" + path[i] + "' not found in " + tag;
35 }
36  
37 // Return the required value
38 if (i === len - 1){
39 return lookup;
40 }
41 }
42 });
43 };
44 }());
45  
46 /**
47 * https://github.com/jeromeetienne/microevent.js
48 * MicroEvent - to make any js object an event emitter (server or browser)
49 *
50 * - pure javascript - server compatible, browser compatible
51 * - dont rely on the browser doms
52 * - super simple - you get it immediatly, no mistery, no magic involved
53 *
54 * - create a MicroEventDebug with goodies to debug
55 * - make it safer to use
56 */
57 DrawingBoard.Utils.MicroEvent = function(){};
58  
59 DrawingBoard.Utils.MicroEvent.prototype = {
60 bind : function(event, fct){
61 this._events = this._events || {};
62 this._events[event] = this._events[event] || [];
63 this._events[event].push(fct);
64 },
65 unbind : function(event, fct){
66 this._events = this._events || {};
67 if( event in this._events === false ) return;
68 this._events[event].splice(this._events[event].indexOf(fct), 1);
69 },
70 trigger : function(event /* , args... */){
71 this._events = this._events || {};
72 if( event in this._events === false ) return;
73 for(var i = 0; i < this._events[event].length; i++){
74 this._events[event][i].apply(this, Array.prototype.slice.call(arguments, 1));
75 }
76 }
77 };
78  
79 //I know.
80 DrawingBoard.Utils._boxBorderSize = function($el, withPadding, withMargin, direction) {
81 withPadding = !!withPadding || true;
82 withMargin = !!withMargin || false;
83 var width = 0,
84 props;
85 if (direction == "width") {
86 props = ['border-left-width', 'border-right-width'];
87 if (withPadding) props.push('padding-left', 'padding-right');
88 if (withMargin) props.push('margin-left', 'margin-right');
89 } else {
90 props = ['border-top-width', 'border-bottom-width'];
91 if (withPadding) props.push('padding-top', 'padding-bottom');
92 if (withMargin) props.push('margin-top', 'margin-bottom');
93 }
94 for (var i = props.length - 1; i >= 0; i--)
95 width += parseInt($el.css(props[i]).replace('px', ''), 10);
96 return width;
97 };
98  
99 DrawingBoard.Utils.boxBorderWidth = function($el, withPadding, withMargin) {
100 return DrawingBoard.Utils._boxBorderSize($el, withPadding, withMargin, 'width');
101 };
102  
103 DrawingBoard.Utils.boxBorderHeight = function($el, withPadding, withMargin) {
104 return DrawingBoard.Utils._boxBorderSize($el, withPadding, withMargin, 'height');
105 };
106  
107 DrawingBoard.Utils.isColor = function(string) {
108 if (!string || !string.length) return false;
109 return (/(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i).test(string) || $.inArray(string.substring(0, 3), ['rgb', 'hsl']) !== -1;
110 };
111  
112 /**
113 * Packs an RGB color into a single integer.
114 */
115 DrawingBoard.Utils.RGBToInt = function(r, g, b) {
116 var c = 0;
117 c |= (r & 255) << 16;
118 c |= (g & 255) << 8;
119 c |= (b & 255);
120 return c;
121 };
122  
123 /**
124 * Returns informations on the pixel located at (x,y).
125 */
126 DrawingBoard.Utils.pixelAt = function(image, x, y) {
127 var i = (y * image.width + x) * 4;
128 var c = DrawingBoard.Utils.RGBToInt(
129 image.data[i],
130 image.data[i + 1],
131 image.data[i + 2]
132 );
133  
134 return [
135 i, // INDEX
136 x, // X
137 y, // Y
138 c // COLOR
139 ];
140 };
141  
142 /**
143 * Compares two colors with the given tolerance (between 0 and 255).
144 */
145 DrawingBoard.Utils.compareColors = function(a, b, tolerance) {
146 if (tolerance === 0) {
147 return (a === b);
148 }
149  
150 var ra = (a >> 16) & 255, rb = (b >> 16) & 255,
151 ga = (a >> 8) & 255, gb = (b >> 8) & 255,
152 ba = a & 255, bb = b & 255;
153  
154 return (Math.abs(ra - rb) <= tolerance)
155 && (Math.abs(ga - gb) <= tolerance)
156 && (Math.abs(ba - bb) <= tolerance);
157 };
158  
159 (function() {
160 var lastTime = 0;
161 var vendors = ['ms', 'moz', 'webkit', 'o'];
162 for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
163 window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
164 window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame'];
165 }
166 }());