Jump to content

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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

The original code works in most browsers Chrome,IE11 +, Edge, Opera, but! Not Firefox while the code from op works in all these browsers.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...