Skip to content

Commit 80e362b

Browse files
committed
v.1.2.3
* conicGradient without HALF_PI * pattern.getColorAt return color * transform correctly applied
1 parent 70c76f9 commit 80e362b

File tree

4 files changed

+51
-34
lines changed

4 files changed

+51
-34
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Class to create **linear, radial, conic and elliptic gradients** and **image patterns** as bitmaps without canvas
44

5-
**version 1.2.2** (13 kB minified)
5+
**version 1.2.3** (13 kB minified)
66

77
**API:**
88

src/Gradient.js

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Gradient
33
* class to create linear/radial/elliptical/conic gradients as bitmaps even without canvas
44
*
5-
* @version 1.2.2
5+
* @version 1.2.3
66
* https://github.com/foo123/Gradient
77
*
88
**/
@@ -64,8 +64,9 @@ function Gradient(grad_color_at)
6464
};
6565
self.getColorAt = function(x, y) {
6666
var im = transform.imatrix(true),
67-
p = im ? im.transform(x, y) : null;
68-
return p ? grad_color_at(p.x, p.y, colorStops(), new ImArray(4), 0) : new ImArray(4);
67+
p = im ? im.transform(x, y) : null,
68+
rgba = new ImArray(4);
69+
return p ? grad_color_at(p.x, p.y, colorStops(), rgba, 0) : rgba;
6970
};
7071
self.getBitmap = function(width, height) {
7172
width = stdMath.round(width);
@@ -88,7 +89,7 @@ function Gradient(grad_color_at)
8889
return bmp;
8990
};
9091
}
91-
Gradient.VERSION = "1.2.2";
92+
Gradient.VERSION = "1.2.3";
9293
Gradient.prototype = {
9394
constructor: Gradient,
9495
transform: null,
@@ -199,7 +200,7 @@ Gradient.createConicGradient = function(angle, cx, cy) {
199200
cy = cy || 0;
200201
return new Gradient(function(x, y, stops, pixel, i) {
201202
var t, stop1, stop2, sl = stops.length;
202-
t = stdMath.atan2(y - cy, x - cx) + HALF_PI - angle;
203+
t = stdMath.atan2(y - cy, x - cx) /*+ HALF_PI*/ - angle;
203204
if (0 > t) t += TWO_PI;
204205
if (t > TWO_PI) t -= TWO_PI;
205206
t = clamp(t/TWO_PI, 0, 1);
@@ -266,8 +267,13 @@ function Pattern(pat_color_at)
266267
configurable: false
267268
});
268269
self.getColorAt = function(x, y) {
269-
var p = transform.imatrix(true).transform(x, y);
270-
return pat_color_at(p.x, p.y, new ImArray(4), 0);
270+
var im = transform.imatrix(true), p, rgba = new ImArray(4);
271+
if (im)
272+
{
273+
p = im.transform(x, y);
274+
pat_color_at(p.x, p.y, rgba, 0);
275+
}
276+
return rgba;
271277
};
272278
self.getBitmap = function(width, height) {
273279
width = stdMath.round(width);
@@ -276,11 +282,14 @@ function Pattern(pat_color_at)
276282
i, x, y, p,
277283
size = (width*height) << 2,
278284
bmp = new ImArray(size);
279-
for (x=0,y=0,i=0; i<size; i+=4,++x)
285+
if (imatrix)
280286
{
281-
if (x >= width) {x=0; ++y;}
282-
p = imatrix.transform(x, y);
283-
pat_color_at(p.x, p.y, bmp, i);
287+
for (x=0,y=0,i=0; i<size; i+=4,++x)
288+
{
289+
if (x >= width) {x=0; ++y;}
290+
p = imatrix.transform(x, y);
291+
pat_color_at(p.x, p.y, bmp, i);
292+
}
284293
}
285294
return bmp;
286295
};
@@ -309,6 +318,7 @@ Pattern.createPattern = function(imageData, repetition) {
309318
pixel[i + 2] = imageData.data[j + 2];
310319
pixel[i + 3] = imageData.data[j + 3];
311320
}
321+
return pixel;
312322
});
313323
case 'repeat-x':
314324
return new Pattern(function(x, y, pixel, i) {
@@ -324,6 +334,7 @@ Pattern.createPattern = function(imageData, repetition) {
324334
pixel[i + 2] = imageData.data[j + 2];
325335
pixel[i + 3] = imageData.data[j + 3];
326336
}
337+
return pixel;
327338
});
328339
case 'repeat-y':
329340
return new Pattern(function(x, y, pixel, i) {
@@ -339,6 +350,7 @@ Pattern.createPattern = function(imageData, repetition) {
339350
pixel[i + 2] = imageData.data[j + 2];
340351
pixel[i + 3] = imageData.data[j + 3];
341352
}
353+
return pixel;
342354
});
343355
case 'repeat':
344356
default:
@@ -354,6 +366,7 @@ Pattern.createPattern = function(imageData, repetition) {
354366
pixel[i + 1] = imageData.data[j + 1];
355367
pixel[i + 2] = imageData.data[j + 2];
356368
pixel[i + 3] = imageData.data[j + 3];
369+
return pixel;
357370
});
358371
}
359372
}
@@ -404,44 +417,48 @@ function Transform()
404417
return self;
405418
};
406419
self.scale = function(sx, sy, ox, oy) {
407-
matrix = Matrix.scale(sx, sy, ox, oy).mul(matrix);
408-
imatrix = imatrix.mul(Matrix.scale(1/sx, 1/sy, ox, oy));
420+
matrix = matrix.mul(Matrix.scale(sx, sy, ox, oy));
421+
imatrix = Matrix.scale(1/sx, 1/sy, ox, oy).mul(imatrix);
409422
return self;
410423
};
411424
self.rotate = function(theta, ox, oy) {
412-
matrix = Matrix.rotate(theta, ox, oy).mul(matrix);
413-
imatrix = imatrix.mul(Matrix.rotate(-theta, ox, oy));
425+
matrix = matrix.mul(Matrix.rotate(theta, ox, oy));
426+
imatrix = Matrix.rotate(-theta, ox, oy).mul(imatrix);
414427
return self;
415428
};
416429
self.translate = function(tx, ty) {
417-
matrix = Matrix.translate(tx, ty).mul(matrix);
418-
imatrix = imatrix.mul(Matrix.translate(-tx, -ty));
430+
matrix = matrix.mul(Matrix.translate(tx, ty));
431+
imatrix = Matrix.translate(-tx, -ty).mul(imatrix);
419432
return self;
420433
};
421434
self.reflectX = function(s) {
422-
matrix = Matrix.reflectX().mul(matrix);
423-
imatrix = imatrix.mul(Matrix.reflectX());
435+
var m = Matrix.reflectX();
436+
matrix = matrix.mul(m);
437+
imatrix = m.mul(imatrix);
424438
return self;
425439
};
426440
self.reflectY = function(s) {
427-
matrix = Matrix.reflectY().mul(matrix);
428-
imatrix = imatrix.mul(Matrix.reflectY());
441+
var m = Matrix.reflectY();
442+
matrix = matrix.mul(m);
443+
imatrix = m.mul(imatrix);
429444
return self;
430445
};
431446
self.skewX = function(s) {
432-
matrix = Matrix.skewX(s).mul(matrix);
433-
imatrix = imatrix.mul(Matrix.skewX(s).inv());
447+
var m = Matrix.skewX(s);
448+
matrix = matrix.mul(m);
449+
imatrix = m.inv().mul(imatrix);
434450
return self;
435451
};
436452
self.skewY = function(s) {
437-
matrix = Matrix.skewY(s).mul(matrix);
438-
imatrix = imatrix.mul(Matrix.skewY(s).inv());
453+
var m = Matrix.skewY(s);
454+
matrix = matrix.mul(m);
455+
imatrix = m.inv().mul(imatrix);
439456
return self;
440457
};
441458
self.transform = function(a, b, c, d, e, f) {
442459
var m = new Matrix(a, c, e, b, d, f);
443-
matrix = m.mul(matrix);
444-
imatrix = imatrix.mul(m.inv());
460+
matrix = matrix.mul(m);
461+
imatrix = m.inv().mul(imatrix);
445462
return self;
446463
};
447464

@@ -590,9 +607,9 @@ function is_strictly_equal(a, b)
590607
{
591608
return stdMath.abs(a - b) < Number.EPSILON;
592609
}
593-
function is_almost_equal(a, b)
610+
function is_almost_equal(a, b, eps)
594611
{
595-
return stdMath.abs(a - b) < EPS;
612+
return stdMath.abs(a - b) < (eps || EPS);
596613
}
597614
function clamp(x, xmin, xmax)
598615
{
@@ -606,7 +623,7 @@ function quadratic_roots(a, b, c)
606623
{
607624
if (is_strictly_equal(a, 0)) return linear_roots(b, c);
608625
var D = b*b - 4*a*c, DS = 0;
609-
if (is_almost_equal(D, 0)) return [-b/(2*a)];
626+
if (is_almost_equal(D, 0, 1e-6)) return [-b/(2*a)];
610627
if (0 > D) return false;
611628
DS = stdMath.sqrt(D);
612629
return [(-b-DS)/(2*a), (-b+DS)/(2*a)];

0 commit comments

Comments
 (0)