Skip to content

Commit

Permalink
Merge pull request #7 from x4d3/main
Browse files Browse the repository at this point in the history
Fix adding CanvasPattern
  • Loading branch information
zenozeng authored Nov 13, 2021
2 parents 218b3d3 + f8d0aa0 commit e23938e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 19 deletions.
16 changes: 11 additions & 5 deletions context.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ export default (function () {
})
}

var keys = Object.keys(STYLES), i, style, value, id, regex, matches;
var keys = Object.keys(STYLES), i, style, value, regex, matches, id, nodeIndex, node;
for (i = 0; i < keys.length; i++) {
style = STYLES[keys[i]];
value = this[keys[i]];
Expand All @@ -381,10 +381,11 @@ export default (function () {
//pattern
if (value.__ctx) {
//copy over defs
while(value.__ctx.__defs.childNodes.length) {
id = value.__ctx.__defs.childNodes[0].getAttribute("id");
this.__ids[id] = id;
this.__defs.appendChild(value.__ctx.__defs.childNodes[0]);
for(nodeIndex = 0; nodeIndex < value.__ctx.__defs.childNodes.length; nodeIndex++){
node = value.__ctx.__defs.childNodes[nodeIndex];
id = node.getAttribute("id");
this.__ids[id] = id;
this.__defs.appendChild(node);
}
}
currentElement.setAttribute(style.apply, format("url(#{id})", {id:value.__root.getAttribute("id")}));
Expand Down Expand Up @@ -1155,6 +1156,11 @@ export default (function () {
pattern.setAttribute("id", id);
pattern.setAttribute("width", image.width);
pattern.setAttribute("height", image.height);
// We want the pattern sizing to be absolute, and not relative
// https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Patterns
// https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/patternUnits
pattern.setAttribute("patternUnits", "userSpaceOnUse");

if (image.nodeName === "CANVAS" || image.nodeName === "IMG") {
img = this.__document.createElementNS("http://www.w3.org/2000/svg", "image");
img.setAttribute("width", image.width);
Expand Down
30 changes: 16 additions & 14 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,26 @@ import setLineDash from './tests/setLineDash'
import text from './tests/text'
import tiger from './tests/tiger'
import transform from './tests/transform'
import pattern from "./tests/pattern";

const tests = [
tiger,
arc,
arcTo,
arcTo2,
emptyArc,
fillstyle,
globalAlpha,
gradient,
linecap,
linewidth,
rgba,
rotate,
saveandrestore,
arc,
arcTo,
arcTo2,
emptyArc,
fillstyle,
globalAlpha,
gradient,
linecap,
linewidth,
rgba,
rotate,
saveandrestore,
setLineDash,
text,
transform
transform,
pattern
];

for (let fn of tests) {
Expand All @@ -56,4 +58,4 @@ for (let fn of tests) {
const ctx = c.getContext('2d');
fn(ctx);
}
}
}
Binary file added test/pattern.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions test/tests/pattern.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export default function pattern(ctx) {
// Create a pattern
const patternCanvas = document.createElement('canvas');
const patternContext = patternCanvas.getContext('2d');

// Give the pattern a width and height of 50
patternCanvas.width = 50;
patternCanvas.height = 50;

// Give the pattern a background color and draw an arc
patternContext.fillStyle = '#fec';
patternContext.fillRect(0, 0, patternCanvas.width, patternCanvas.height);
patternContext.arc(0, 0, 50, 0, .5 * Math.PI);
patternContext.stroke();

const pattern = ctx.createPattern(patternCanvas, 'repeat');
ctx.fillStyle = pattern;
ctx.fillRect(0, 0, 50, 50);

var img = new Image();
img.src = 'pattern.png';
img.onload = function() {
var pattern = ctx.createPattern(img, 'repeat');
ctx.fillStyle = pattern;
ctx.fillRect(50, 50, 300, 300);
};

};

0 comments on commit e23938e

Please sign in to comment.