Skip to content

Commit

Permalink
v.1.2.3
Browse files Browse the repository at this point in the history
* conicGradient without HALF_PI
* pattern.getColorAt return color
* transform correctly applied
  • Loading branch information
foo123 committed Jul 31, 2023
1 parent 70c76f9 commit 80e362b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

**version 1.2.2** (13 kB minified)
**version 1.2.3** (13 kB minified)

**API:**

Expand Down
77 changes: 47 additions & 30 deletions src/Gradient.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Gradient
* class to create linear/radial/elliptical/conic gradients as bitmaps even without canvas
*
* @version 1.2.2
* @version 1.2.3
* https://github.com/foo123/Gradient
*
**/
Expand Down Expand Up @@ -64,8 +64,9 @@ function Gradient(grad_color_at)
};
self.getColorAt = function(x, y) {
var im = transform.imatrix(true),
p = im ? im.transform(x, y) : null;
return p ? grad_color_at(p.x, p.y, colorStops(), new ImArray(4), 0) : new ImArray(4);
p = im ? im.transform(x, y) : null,
rgba = new ImArray(4);
return p ? grad_color_at(p.x, p.y, colorStops(), rgba, 0) : rgba;
};
self.getBitmap = function(width, height) {
width = stdMath.round(width);
Expand All @@ -88,7 +89,7 @@ function Gradient(grad_color_at)
return bmp;
};
}
Gradient.VERSION = "1.2.2";
Gradient.VERSION = "1.2.3";
Gradient.prototype = {
constructor: Gradient,
transform: null,
Expand Down Expand Up @@ -199,7 +200,7 @@ Gradient.createConicGradient = function(angle, cx, cy) {
cy = cy || 0;
return new Gradient(function(x, y, stops, pixel, i) {
var t, stop1, stop2, sl = stops.length;
t = stdMath.atan2(y - cy, x - cx) + HALF_PI - angle;
t = stdMath.atan2(y - cy, x - cx) /*+ HALF_PI*/ - angle;
if (0 > t) t += TWO_PI;
if (t > TWO_PI) t -= TWO_PI;
t = clamp(t/TWO_PI, 0, 1);
Expand Down Expand Up @@ -266,8 +267,13 @@ function Pattern(pat_color_at)
configurable: false
});
self.getColorAt = function(x, y) {
var p = transform.imatrix(true).transform(x, y);
return pat_color_at(p.x, p.y, new ImArray(4), 0);
var im = transform.imatrix(true), p, rgba = new ImArray(4);
if (im)
{
p = im.transform(x, y);
pat_color_at(p.x, p.y, rgba, 0);
}
return rgba;
};
self.getBitmap = function(width, height) {
width = stdMath.round(width);
Expand All @@ -276,11 +282,14 @@ function Pattern(pat_color_at)
i, x, y, p,
size = (width*height) << 2,
bmp = new ImArray(size);
for (x=0,y=0,i=0; i<size; i+=4,++x)
if (imatrix)
{
if (x >= width) {x=0; ++y;}
p = imatrix.transform(x, y);
pat_color_at(p.x, p.y, bmp, i);
for (x=0,y=0,i=0; i<size; i+=4,++x)
{
if (x >= width) {x=0; ++y;}
p = imatrix.transform(x, y);
pat_color_at(p.x, p.y, bmp, i);
}
}
return bmp;
};
Expand Down Expand Up @@ -309,6 +318,7 @@ Pattern.createPattern = function(imageData, repetition) {
pixel[i + 2] = imageData.data[j + 2];
pixel[i + 3] = imageData.data[j + 3];
}
return pixel;
});
case 'repeat-x':
return new Pattern(function(x, y, pixel, i) {
Expand All @@ -324,6 +334,7 @@ Pattern.createPattern = function(imageData, repetition) {
pixel[i + 2] = imageData.data[j + 2];
pixel[i + 3] = imageData.data[j + 3];
}
return pixel;
});
case 'repeat-y':
return new Pattern(function(x, y, pixel, i) {
Expand All @@ -339,6 +350,7 @@ Pattern.createPattern = function(imageData, repetition) {
pixel[i + 2] = imageData.data[j + 2];
pixel[i + 3] = imageData.data[j + 3];
}
return pixel;
});
case 'repeat':
default:
Expand All @@ -354,6 +366,7 @@ Pattern.createPattern = function(imageData, repetition) {
pixel[i + 1] = imageData.data[j + 1];
pixel[i + 2] = imageData.data[j + 2];
pixel[i + 3] = imageData.data[j + 3];
return pixel;
});
}
}
Expand Down Expand Up @@ -404,44 +417,48 @@ function Transform()
return self;
};
self.scale = function(sx, sy, ox, oy) {
matrix = Matrix.scale(sx, sy, ox, oy).mul(matrix);
imatrix = imatrix.mul(Matrix.scale(1/sx, 1/sy, ox, oy));
matrix = matrix.mul(Matrix.scale(sx, sy, ox, oy));
imatrix = Matrix.scale(1/sx, 1/sy, ox, oy).mul(imatrix);
return self;
};
self.rotate = function(theta, ox, oy) {
matrix = Matrix.rotate(theta, ox, oy).mul(matrix);
imatrix = imatrix.mul(Matrix.rotate(-theta, ox, oy));
matrix = matrix.mul(Matrix.rotate(theta, ox, oy));
imatrix = Matrix.rotate(-theta, ox, oy).mul(imatrix);
return self;
};
self.translate = function(tx, ty) {
matrix = Matrix.translate(tx, ty).mul(matrix);
imatrix = imatrix.mul(Matrix.translate(-tx, -ty));
matrix = matrix.mul(Matrix.translate(tx, ty));
imatrix = Matrix.translate(-tx, -ty).mul(imatrix);
return self;
};
self.reflectX = function(s) {
matrix = Matrix.reflectX().mul(matrix);
imatrix = imatrix.mul(Matrix.reflectX());
var m = Matrix.reflectX();
matrix = matrix.mul(m);
imatrix = m.mul(imatrix);
return self;
};
self.reflectY = function(s) {
matrix = Matrix.reflectY().mul(matrix);
imatrix = imatrix.mul(Matrix.reflectY());
var m = Matrix.reflectY();
matrix = matrix.mul(m);
imatrix = m.mul(imatrix);
return self;
};
self.skewX = function(s) {
matrix = Matrix.skewX(s).mul(matrix);
imatrix = imatrix.mul(Matrix.skewX(s).inv());
var m = Matrix.skewX(s);
matrix = matrix.mul(m);
imatrix = m.inv().mul(imatrix);
return self;
};
self.skewY = function(s) {
matrix = Matrix.skewY(s).mul(matrix);
imatrix = imatrix.mul(Matrix.skewY(s).inv());
var m = Matrix.skewY(s);
matrix = matrix.mul(m);
imatrix = m.inv().mul(imatrix);
return self;
};
self.transform = function(a, b, c, d, e, f) {
var m = new Matrix(a, c, e, b, d, f);
matrix = m.mul(matrix);
imatrix = imatrix.mul(m.inv());
matrix = matrix.mul(m);
imatrix = m.inv().mul(imatrix);
return self;
};

Expand Down Expand Up @@ -590,9 +607,9 @@ function is_strictly_equal(a, b)
{
return stdMath.abs(a - b) < Number.EPSILON;
}
function is_almost_equal(a, b)
function is_almost_equal(a, b, eps)
{
return stdMath.abs(a - b) < EPS;
return stdMath.abs(a - b) < (eps || EPS);
}
function clamp(x, xmin, xmax)
{
Expand All @@ -606,7 +623,7 @@ function quadratic_roots(a, b, c)
{
if (is_strictly_equal(a, 0)) return linear_roots(b, c);
var D = b*b - 4*a*c, DS = 0;
if (is_almost_equal(D, 0)) return [-b/(2*a)];
if (is_almost_equal(D, 0, 1e-6)) return [-b/(2*a)];
if (0 > D) return false;
DS = stdMath.sqrt(D);
return [(-b-DS)/(2*a), (-b+DS)/(2*a)];
Expand Down
Loading

0 comments on commit 80e362b

Please sign in to comment.