This is my first experiment with Canvas and getting to draw a pattern image as a background and it works beautifully.
Created
September 27, 2014 22:20
-
-
Save sinrise/599f5a829d4b26bf6a79 to your computer and use it in GitHub Desktop.
A Pen by sinrise.
This file contains hidden or 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
<canvas id="haz-pat" width="72" height="144"></canvas> | |
<canvas id="hazards" width="800"></canvas> |
This file contains hidden or 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
var canvas = document.getElementById("haz-pat"); | |
var context = canvas.getContext("2d"); | |
context.beginPath(); | |
context.moveTo(0,0); | |
context.lineTo(36, 0); | |
context.lineTo(0, 36); | |
context.closePath(); | |
context.fill(); | |
context.beginPath(); | |
context.moveTo(72, 0); | |
context.lineTo(72, 36); | |
context.lineTo(0, 108); | |
context.lineTo(0, 72); | |
context.closePath(); | |
context.fill(); | |
context.beginPath(); | |
context.moveTo(72, 72); | |
context.lineTo(72, 108); | |
context.lineTo(36, 144); | |
context.lineTo(0, 144); | |
context.closePath(); | |
context.fill(); | |
var canvas2 = document.getElementById("hazards"); | |
var context2 = canvas2.getContext("2d"); | |
var myPattern = context2.createPattern(canvas,"repeat"); | |
context2.fillStyle = myPattern; | |
context2.fillRect(0,0, canvas2.width, canvas2.height); | |
context2.fill(); |
This file contains hidden or 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
canvas#haz-pat { | |
display: none; | |
} | |
canvas#hazards { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment