Skip to content

Commit

Permalink
v.0.9.92r2
Browse files Browse the repository at this point in the history
* fix strokeStyle/fillStyle with gradient/pattern
  • Loading branch information
foo123 committed Aug 2, 2023
1 parent b42f21c commit 3403587
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 98 deletions.
59 changes: 10 additions & 49 deletions build/CanvasLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
* CanvasLite
* an html canvas implementation in pure JavaScript
*
* @version 0.9.92r2 (2023-08-02 12:05:01)
* @version 0.9.92r2 (2023-08-02 12:49:06)
* https://github.com/foo123/CanvasLite
*
**//**
* CanvasLite
* an html canvas implementation in pure JavaScript
*
* @version 0.9.92r2 (2023-08-02 12:05:01)
* @version 0.9.92r2 (2023-08-02 12:49:06)
* https://github.com/foo123/CanvasLite
*
**/
Expand Down Expand Up @@ -277,7 +277,7 @@ function RenderingContext2D(width, height, set_rgba_at, get_rgba_from)
set: function(c) {
if (c instanceof Gradient || c instanceof Pattern)
{
get_stroke_at = Rasterizer.getRGBAFrom(c.getColorAt.bind(c));
get_stroke_at = c.getColorAt.bind(c);
}
else
{
Expand All @@ -292,7 +292,7 @@ function RenderingContext2D(width, height, set_rgba_at, get_rgba_from)
set: function(c) {
if (c instanceof Gradient || c instanceof Pattern)
{
get_fill_at = Rasterizer.getRGBAFrom(c.getColorAt.bind(c));
get_fill_at = c.getColorAt.bind(c);
}
else
{
Expand Down Expand Up @@ -3314,37 +3314,16 @@ function Gradient(grad_color_at)
self.getColorAt = function(x, y) {
var im = transform.imatrix(true),
p = im ? im.transform(x, y) : null,
rgba = new ImArray(4);
rgba = [0, 0, 0, 0];
return p ? grad_color_at(p[0], p[1], colorStops(), rgba, 0) : rgba;
};
self.getBitmap = function(width, height) {
width = stdMath.round(width);
height = stdMath.round(height);
var imatrix = transform.imatrix(true),
color_stops,
i, x, y, p,
size = (width*height) << 2,
bmp = new ImArray(size);
if (imatrix)
{
color_stops = colorStops();
for (x=0,y=0,i=0; i<size; i+=4,++x)
{
if (x >= width) {x=0; ++y;}
p = imatrix.transform(x, y);
grad_color_at(p[0], p[1], color_stops, bmp, i);
}
}
return bmp;
};
}
Gradient.VERSION = "1.2.3";
Gradient[PROTO] = {
constructor: Gradient,
transform: null,
addColorStop: null,
getColorAt: null,
getBitmap: null
getColorAt: null
};
Gradient.createLinearGradient = function(x1, y1, x2, y2) {
x1 = x1 || 0;
Expand Down Expand Up @@ -3487,38 +3466,20 @@ function Pattern(pat_color_at)
configurable: false
});
self.getColorAt = function(x, y) {
var im = transform.imatrix(true), p, rgba = new ImArray(4);
var im = transform.imatrix(true), p, rgba = [0, 0, 0, 0];
if (im)
{
p = im.transform(x, y);
pat_color_at(p[0], p[1], rgba, 0);
}
rgba[3] /= 255;
return rgba;
};
self.getBitmap = function(width, height) {
width = stdMath.round(width);
height = stdMath.round(height);
var imatrix = transform.imatrix(true),
i, x, y, p,
size = (width*height) << 2,
bmp = new ImArray(size);
if (imatrix)
{
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[0], p[1], bmp, i);
}
}
return bmp;
};
}
Pattern[PROTO] = {
constructor: Pattern,
transform: null,
getColorAt: null,
getBitmap: null
getColorAt: null
};
Pattern.createPattern = function(imageData, repetition) {
if (imageData && ('function' === typeof imageData.getImageData)) imageData = imageData.getImageData();
Expand Down Expand Up @@ -3733,7 +3694,7 @@ function interpolatePixel(pixel, index, rgba0, rgba1, t)
pixel[index + 0] = clamp(stdMath.round(rgba0[0] + t*(rgba1[0] - rgba0[0])), 0, 255);
pixel[index + 1] = clamp(stdMath.round(rgba0[1] + t*(rgba1[1] - rgba0[1])), 0, 255);
pixel[index + 2] = clamp(stdMath.round(rgba0[2] + t*(rgba1[2] - rgba0[2])), 0, 255);
pixel[index + 3] = 3 < rgba0.length ? clamp(stdMath.round(255*(rgba0[3] + t*(rgba1[3] - rgba0[3]))), 0, 255) : 255;
pixel[index + 3] = 3 < rgba0.length ? clamp(rgba0[3] + t*(rgba1[3] - rgba0[3]), 0, 1) : 1;
return pixel;
}
var hexRE = /^#([0-9a-fA-F]{3,6})\b/,
Expand Down
4 changes: 2 additions & 2 deletions build/CanvasLite.min.js

Large diffs are not rendered by default.

51 changes: 6 additions & 45 deletions src/Gradient.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,37 +48,16 @@ function Gradient(grad_color_at)
self.getColorAt = function(x, y) {
var im = transform.imatrix(true),
p = im ? im.transform(x, y) : null,
rgba = new ImArray(4);
rgba = [0, 0, 0, 0];
return p ? grad_color_at(p[0], p[1], colorStops(), rgba, 0) : rgba;
};
self.getBitmap = function(width, height) {
width = stdMath.round(width);
height = stdMath.round(height);
var imatrix = transform.imatrix(true),
color_stops,
i, x, y, p,
size = (width*height) << 2,
bmp = new ImArray(size);
if (imatrix)
{
color_stops = colorStops();
for (x=0,y=0,i=0; i<size; i+=4,++x)
{
if (x >= width) {x=0; ++y;}
p = imatrix.transform(x, y);
grad_color_at(p[0], p[1], color_stops, bmp, i);
}
}
return bmp;
};
}
Gradient.VERSION = "1.2.3";
Gradient[PROTO] = {
constructor: Gradient,
transform: null,
addColorStop: null,
getColorAt: null,
getBitmap: null
getColorAt: null
};
Gradient.createLinearGradient = function(x1, y1, x2, y2) {
x1 = x1 || 0;
Expand Down Expand Up @@ -221,38 +200,20 @@ function Pattern(pat_color_at)
configurable: false
});
self.getColorAt = function(x, y) {
var im = transform.imatrix(true), p, rgba = new ImArray(4);
var im = transform.imatrix(true), p, rgba = [0, 0, 0, 0];
if (im)
{
p = im.transform(x, y);
pat_color_at(p[0], p[1], rgba, 0);
}
rgba[3] /= 255;
return rgba;
};
self.getBitmap = function(width, height) {
width = stdMath.round(width);
height = stdMath.round(height);
var imatrix = transform.imatrix(true),
i, x, y, p,
size = (width*height) << 2,
bmp = new ImArray(size);
if (imatrix)
{
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[0], p[1], bmp, i);
}
}
return bmp;
};
}
Pattern[PROTO] = {
constructor: Pattern,
transform: null,
getColorAt: null,
getBitmap: null
getColorAt: null
};
Pattern.createPattern = function(imageData, repetition) {
if (imageData && ('function' === typeof imageData.getImageData)) imageData = imageData.getImageData();
Expand Down Expand Up @@ -467,7 +428,7 @@ function interpolatePixel(pixel, index, rgba0, rgba1, t)
pixel[index + 0] = clamp(stdMath.round(rgba0[0] + t*(rgba1[0] - rgba0[0])), 0, 255);
pixel[index + 1] = clamp(stdMath.round(rgba0[1] + t*(rgba1[1] - rgba0[1])), 0, 255);
pixel[index + 2] = clamp(stdMath.round(rgba0[2] + t*(rgba1[2] - rgba0[2])), 0, 255);
pixel[index + 3] = 3 < rgba0.length ? clamp(stdMath.round(255*(rgba0[3] + t*(rgba1[3] - rgba0[3]))), 0, 255) : 255;
pixel[index + 3] = 3 < rgba0.length ? clamp(rgba0[3] + t*(rgba1[3] - rgba0[3]), 0, 1) : 1;
return pixel;
}
var hexRE = /^#([0-9a-fA-F]{3,6})\b/,
Expand Down
4 changes: 2 additions & 2 deletions src/Rasterizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ function RenderingContext2D(width, height, set_rgba_at, get_rgba_from)
set: function(c) {
if (c instanceof Gradient || c instanceof Pattern)
{
get_stroke_at = Rasterizer.getRGBAFrom(c.getColorAt.bind(c));
get_stroke_at = c.getColorAt.bind(c);
}
else
{
Expand All @@ -248,7 +248,7 @@ function RenderingContext2D(width, height, set_rgba_at, get_rgba_from)
set: function(c) {
if (c instanceof Gradient || c instanceof Pattern)
{
get_fill_at = Rasterizer.getRGBAFrom(c.getColorAt.bind(c));
get_fill_at = c.getColorAt.bind(c);
}
else
{
Expand Down

0 comments on commit 3403587

Please sign in to comment.