Jump to content

JemPD

Recommended Posts

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...