Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7953061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:03:06+00:00 2026-06-04T03:03:06+00:00

Is there a way using javascript html5 canvas, to have a polygon and then

  • 0

Is there a way using javascript html5 canvas, to have a polygon and then display an image in it instead of a color?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:03:08+00:00Added an answer on June 4, 2026 at 3:03 am

    After some research, I believe it’s possible to do that by first creating a pattern using your image, then setting that pattern to fillStyle:

    var ctx = canvas.getContext("2d");
    var pattern = ctx.createPattern(imageObj, "repeat");
    ctx.fillStyle = pattern;
    

    Then it’s just a matter of creating your polygon (using moveTo and lineTo) and then filling it normally.

    Source: this plugin‘s source code. Disclaimer: haven’t tried it myself to confirm that works.

    Update: I’m still investigating whether or not you can manipulate the image to fit an arbitrary polygon. In principle, you could use setTransform to do that:

    ctx.save();
    ctx.setTransform(m11, m12, m21, m22, dx, dy);
    ctx.drawImage(imageObj);
    ctx.restore();
    

    Determining the values of setTransform parameters (if it’s possible to do that at all) is the tricky part. It’s been looong since I did any math, but if I recall correctly here’s what needs to be done:

    (0,0) --- (w,0)       (x1,y1) --- (x2,y2)
      |         |            |           |
      |  Image  |     =>     |  Morphed  |
      |         |            |           |
    (0,h) --- (w,h)       (x3,y3) --- (x4,y4)
    

    For each point, you’d do the following matrix operation:

    |m11 m21 dx|   |xI|   |xM|
    |m12 m22 dy| X |yI| = |yM|
    | 0   0   1|   | 1|   | 1|
    

    Eight equations, six variables (remembering that the matrix elements are the variables, the rest are constants – our inputs). Might be unsolvable. Now it’s only a matter of deducing (or googling, or asking in Math.SE…) and implementing the formulas for each parameter…

    Update 2: Although I don’t have hard evidence of that, I believe it’s impossible to do what you want with setTransform. Looking at how Gimp does with its “perspective” tool, it’s necessary to change also the third row of the transform matrix to transform your image to an arbitrary polygon. And the Canvas API does not seem to provide means for that (usually only affine transformations are supported: translation, rotation, scale, shear or a combination of above).

    Quoting this post on 2D transforms:

    CSS3 2D-Transforms can only transform blocks into parallelograms. For example, it is impossible to transform a block into this shape:
    [Irregular Shape]
    In order to do this, one must use CSS3 3D Transforms. This is why the Matrix Construction Set only has three control points to drag around, not four.

    There are plans for CSS 3D Transforms, but not only I don’t know how widely supported that is, I dunno if the canvas element (with 2d context, that is – WebGL is another story) will ever support it. In short, it’s not possible to do what you want through any means I know of.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to cut image into tiles using Javascript in web browser?
1) Is there any way to develop for the Xbox 360 using HTML5/JavaScript? It
Is there a way to access the page HTML source code using javascript? I
Is there any way to completely process a form using only javascript & html
I'm trying to figure out if there is a way (using JavaScript, or jQuery
Is there a way to get the a users screen size/resolutions using javascript? I
Is there a reliable way to access the client machine's clipboard using Javascript? I
Is there a way to draw an arc using points in JavaScript?? I need
I've seen a few JavaScript image rotators that use either the HTML5 canvas element
I'm making another Pong clone using HTML5 canvas and JavaScript. The problem I'm having

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.