forked from gliffy/canvas2svg
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from x4d3/main
Fix adding CanvasPattern
- Loading branch information
Showing
4 changed files
with
55 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
|
||
}; |