diff options
author | Pierre Frisch | 2011-12-22 07:25:50 -0800 |
---|---|---|
committer | Valerio Virgillito | 2012-01-27 11:18:17 -0800 |
commit | b89a7ee8b956c96a1dcee995ea840feddc5d4b27 (patch) | |
tree | 0f3136ab0ecdbbbed6a83576581af0a53124d6f1 /js/components/colorwheel.reel/colorwheel.js | |
parent | 2401f05d1f4b94d45e4568b81fc73e67b969d980 (diff) | |
download | ninja-b89a7ee8b956c96a1dcee995ea840feddc5d4b27.tar.gz |
First commit of Ninja to ninja-internal
Signed-off-by: Valerio Virgillito <rmwh84@motorola.com>
Diffstat (limited to 'js/components/colorwheel.reel/colorwheel.js')
-rw-r--r-- | js/components/colorwheel.reel/colorwheel.js | 536 |
1 files changed, 536 insertions, 0 deletions
diff --git a/js/components/colorwheel.reel/colorwheel.js b/js/components/colorwheel.reel/colorwheel.js new file mode 100644 index 00000000..343422e2 --- /dev/null +++ b/js/components/colorwheel.reel/colorwheel.js | |||
@@ -0,0 +1,536 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | //////////////////////////////////////////////////////////////////////// | ||
8 | // | ||
9 | var Montage = require("montage/core/core").Montage, | ||
10 | Component = require("montage/ui/component").Component; | ||
11 | //////////////////////////////////////////////////////////////////////// | ||
12 | // | ||
13 | exports.ColorWheel = Montage.create(Component, { | ||
14 | //////////////////////////////////////////////////////////////////// | ||
15 | // | ||
16 | hasTemplate: { | ||
17 | enumerable: false, | ||
18 | value: true | ||
19 | }, | ||
20 | //////////////////////////////////////////////////////////////////// | ||
21 | //Value of wheel in HSV (360, 100, 100) | ||
22 | _value: { | ||
23 | enumerable: false, | ||
24 | value: {h: 0, s: 0, v: 0} | ||
25 | }, | ||
26 | //////////////////////////////////////////////////////////////////// | ||
27 | //Value of wheel in HSV (360, 100, 100) | ||
28 | value: { | ||
29 | enumerable: false, | ||
30 | get: function() { | ||
31 | return this._value; | ||
32 | }, | ||
33 | set: function(value) { | ||
34 | this._value = value; | ||
35 | if (this._wheelData) { | ||
36 | if (value && !value.wasSetByCode) { | ||
37 | this.wheelSelectorAngle(value.h/this.element._component.math.TAU*360); | ||
38 | this.drawSwatchColor(value.h/this.element._component.math.TAU*360); | ||
39 | this.drawSwatchSelector(value.s*100, value.v*100); | ||
40 | } | ||
41 | if (!this._isMouseDown) { | ||
42 | this._dispatchEvent('change', true); | ||
43 | } | ||
44 | } | ||
45 | } | ||
46 | }, | ||
47 | //////////////////////////////////////////////////////////////////// | ||
48 | //Stroke size of wheel | ||
49 | _strokeWidth: { | ||
50 | enumerable: false, | ||
51 | value: 2 | ||
52 | }, | ||
53 | //////////////////////////////////////////////////////////////////// | ||
54 | //Size must be set in digits and interpreted as pixel | ||
55 | strokeWidth: { | ||
56 | enumerable: false, | ||
57 | get: function() { | ||
58 | return this._strokeWidth; | ||
59 | }, | ||
60 | set: function(value) { | ||
61 | this._strokeWidth = value; | ||
62 | } | ||
63 | }, | ||
64 | //////////////////////////////////////////////////////////////////// | ||
65 | //Stroke color of wheel | ||
66 | _strokeColor: { | ||
67 | enumerable: false, | ||
68 | value: 'rgb(255, 255, 255)' | ||
69 | }, | ||
70 | //////////////////////////////////////////////////////////////////// | ||
71 | //Stroke only apply to wheel rim | ||
72 | strokeColor: { | ||
73 | enumerable: false, | ||
74 | get: function() { | ||
75 | return this._strokeColor; | ||
76 | }, | ||
77 | set: function(value) { | ||
78 | this._strokeColor = value; | ||
79 | } | ||
80 | }, | ||
81 | //////////////////////////////////////////////////////////////////// | ||
82 | //Width of the rim | ||
83 | _rimWidth: { | ||
84 | enumerable: false, | ||
85 | value: 2 | ||
86 | }, | ||
87 | //////////////////////////////////////////////////////////////////// | ||
88 | //Width must be set using digits interpreted as pixel | ||
89 | rimWidth: { | ||
90 | enumerable: false, | ||
91 | get: function() { | ||
92 | return this._rimWidth; | ||
93 | }, | ||
94 | set: function(value) { | ||
95 | this._rimWidth = value; | ||
96 | } | ||
97 | }, | ||
98 | //////////////////////////////////////////////////////////////////// | ||
99 | // | ||
100 | prepareForDraw: { | ||
101 | enumerable: false, | ||
102 | value: function() { | ||
103 | // | ||
104 | this.element._component = {wheel: {}, swatch: {}, wheel_select: {}, swatch_select: {}, math: {}}; | ||
105 | // | ||
106 | this.element._component.math.PI = Math.PI, | ||
107 | this.element._component.math.TAU = Math.PI*2, | ||
108 | this.element._component.math.RADIANS = Math.PI/180; | ||
109 | } | ||
110 | }, | ||
111 | //////////////////////////////////////////////////////////////////// | ||
112 | // | ||
113 | willDraw: { | ||
114 | enumerable: false, | ||
115 | value: function() { | ||
116 | // | ||
117 | this.element.style.opacity = 0; | ||
118 | // | ||
119 | var cnvs = this.element.getElementsByTagName('canvas'); | ||
120 | // | ||
121 | this.element._component.wheel.canvas = cnvs[0]; | ||
122 | this.element._component.swatch.canvas = cnvs[1]; | ||
123 | this.element._component.wheel_select.canvas = cnvs[3]; | ||
124 | this.element._component.swatch_select.canvas = cnvs[2]; | ||
125 | // | ||
126 | this.element._component.wheel.context = this.element._component.wheel.canvas.getContext("2d"); | ||
127 | this.element._component.swatch.context = this.element._component.swatch.canvas.getContext("2d"); | ||
128 | this.element._component.wheel_select.context = this.element._component.wheel_select.canvas.getContext("2d"); | ||
129 | this.element._component.swatch_select.context = this.element._component.swatch_select.canvas.getContext("2d"); | ||
130 | } | ||
131 | }, | ||
132 | //////////////////////////////////////////////////////////////////// | ||
133 | // | ||
134 | draw: { | ||
135 | enumerable: false, | ||
136 | value: function() { | ||
137 | // | ||
138 | var slice, i, whlctx = this.element._component.wheel.context, math = this.element._component.math, | ||
139 | whlcvs = this.element._component.wheel.canvas, swhcvs = this.element._component.swatch.canvas, | ||
140 | wslcvs = this.element._component.wheel_select.canvas, swscvs = this.element._component.swatch_select.canvas; | ||
141 | //Determing radius by smallest factor of width or height | ||
142 | if (this.element.offsetWidth > this.element.offsetHeight) { | ||
143 | math.diameter = this.element.offsetWidth; | ||
144 | } else { | ||
145 | math.diameter = this.element.offsetHeight; | ||
146 | } | ||
147 | //Setting the radius from diameter | ||
148 | math.radius = math.diameter/2; | ||
149 | //Inner radius of wheel | ||
150 | math.innerRadius = math.radius - this.rimWidth; | ||
151 | //Setting the widths and heights to match the container's | ||
152 | whlcvs.width = whlcvs.height = wslcvs.width = wslcvs.height = swscvs.width = swscvs.height = math.diameter; | ||
153 | // | ||
154 | math.swatchLength = Math.floor((math.radius - this.rimWidth - this.strokeWidth*4) * Math.sin(45 * math.RADIANS) * 2); | ||
155 | math.swatchPosition = Math.round(math.radius - (math.swatchLength/2))+this.strokeWidth* Math.sin(45 * math.RADIANS); | ||
156 | // | ||
157 | //swhcvs.width = swhcvs.height = math.swatchLength; //TODO: Figure out why this breaks on WINDOWS ONLY | ||
158 | swhcvs.style.top = swhcvs.style.left = math.swatchPosition+'px'; | ||
159 | //Clearing wheel for redraw | ||
160 | whlctx.clearRect(0, 0, math.diameter, math.diameter); | ||
161 | //////////////////////////////////////////////////////////////// | ||
162 | //Drawing color wheel circle | ||
163 | whlctx.save(); | ||
164 | whlctx.beginPath(); | ||
165 | whlctx.moveTo(0,0); | ||
166 | whlctx.lineTo(math.diameter,0); | ||
167 | whlctx.lineTo(math.diameter,math.diameter); | ||
168 | whlctx.lineTo(0,math.diameter); | ||
169 | whlctx.closePath(); | ||
170 | whlctx.clip(); | ||
171 | whlctx.strokeStyle = 'rgba(0,0,0,0)'; | ||
172 | whlctx.lineCap = 'butt'; | ||
173 | whlctx.lineJoin = 'miter'; | ||
174 | whlctx.miterLimit = 4; | ||
175 | whlctx.save(); | ||
176 | //////////////////////////////////////////////////////////////// | ||
177 | //Looping through set intervals | ||
178 | math.radius = math.radius - this.strokeWidth/2; | ||
179 | for (i=0; i<60; i++) { | ||
180 | //Calculating slice number | ||
181 | slice = Math.round(255*i/60); | ||
182 | //Drawing 6 slices (6 colors in color theory) | ||
183 | this._drawWheelSlice (i, 255, slice, 0); | ||
184 | this._drawWheelSlice (i+60, 255-slice, 255, 0); | ||
185 | this._drawWheelSlice (i+120, 0, 255, slice); | ||
186 | this._drawWheelSlice (i+180, 0, 255-slice, 255); | ||
187 | this._drawWheelSlice (i+240, slice, 0, 255); | ||
188 | this._drawWheelSlice (i+300, 255, 0, 255-slice); | ||
189 | } | ||
190 | // | ||
191 | math.radius = math.radius + this.strokeWidth/2; | ||
192 | // | ||
193 | whlctx.strokeStyle = this.strokeColor; | ||
194 | whlctx.lineWidth = this.strokeWidth; | ||
195 | whlctx.globalAlpha = 1; | ||
196 | whlctx.beginPath(); | ||
197 | whlctx.arc(math.radius, math.radius, math.radius-this.strokeWidth/2, 0, math.TAU, true); | ||
198 | whlctx.closePath(); | ||
199 | whlctx.stroke(); | ||
200 | whlctx.restore(); | ||
201 | // | ||
202 | whlctx.strokeStyle = this.strokeColor; | ||
203 | whlctx.lineWidth = this.strokeWidth; | ||
204 | whlctx.globalAlpha = 1; | ||
205 | whlctx.beginPath(); | ||
206 | whlctx.arc(math.innerRadius+this.rimWidth, math.innerRadius+this.rimWidth, math.innerRadius-this.strokeWidth/2, 0, math.TAU, true); | ||
207 | whlctx.closePath(); | ||
208 | whlctx.stroke(); | ||
209 | whlctx.restore(); | ||
210 | // | ||
211 | whlctx.beginPath(); | ||
212 | whlctx.moveTo(math.swatchPosition-this.strokeWidth/2, math.swatchPosition-this.strokeWidth/2); | ||
213 |