JemPD Posted April 25, 2017 Share Posted April 25, 2017 The example shown on fillStyle for patterns is wrong: This: var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); var img=document.getElementById("lamp"); var pat=ctx.createPattern(img,"repeat");ctx.rect(0,0,150,100); ctx.fillStyle=pat; ctx.fill(); Should be this: var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); var img=document.getElementById("lamp"); var pat=ctx.createPattern(img,"repeat");ctx.fillStyle=pat; ctx.fillRect(0,0,150,100); This is the only way the repeat keywords work as they should. p.s. didn't know where else to give feedback on this. Link to comment Share on other sites More sharing options...
Ingolme Posted April 25, 2017 Share Posted April 25, 2017 There's a "Report Error" at the bottom of the page, but if you test the code in this editor you'll see that it works: https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_canvas_createpattern That's because the fillStyle property is used by the fill() method, not the rect() method. The fill() method fills shapes that were created earlier in the code. Link to comment Share on other sites More sharing options...
dsonesuk Posted April 25, 2017 Share Posted April 25, 2017 The original code works in most browsers Chrome,IE11 +, Edge, Opera, but! Not Firefox while the code from op works in all these browsers. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now