最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - HTML 5 Skew code - Stack Overflow

matteradmin7PV0评论

I need to simulate the effect of skewing an image, like Photoshop, using Javascript. Do you guys know how to do that in Javascript?

What kind of geometry calculation is applied to the <canvas> context? I can't use CSS for this, because it has to be modified dynamically.

I need to simulate the effect of skewing an image, like Photoshop, using Javascript. Do you guys know how to do that in Javascript?

What kind of geometry calculation is applied to the <canvas> context? I can't use CSS for this, because it has to be modified dynamically.

Share Improve this question edited Jul 16, 2012 at 20:53 ThinkingStiff 65.4k30 gold badges147 silver badges241 bronze badges asked Jul 16, 2012 at 19:17 DuckDuck 36.1k48 gold badges268 silver badges490 bronze badges 2
  • This can be done with CSS3 alone. – Madara's Ghost Commented Jul 16, 2012 at 19:24
  • this cannot be done with CSS because it has to be modified on the fly by javascript. I said that on the question. Thanks anyway. – Duck Commented Jul 16, 2012 at 20:43
Add a ment  | 

3 Answers 3

Reset to default 2

This is possible with CSS3 alone:

img {
  -webkit-transform: skew(45deg);
}

Live Example

Note, I only used webkit, but it's the same for all other modern browsers.

CSS transform: skew(); will do this for you with no Javascript.

Demo: http://jsfiddle/ThinkingStiff/MtWBy/

Output

CSS

#original {
    margin: 55px 10px 0 10px;
}

#vertical {
    margin: 30px 20px 0 0;
    transform:             skew( 0, -30deg );
        -ms-transform:     skew( 0, -30deg );
        -moz-transform:    skew( 0, -30deg );
        -o-transform:      skew( 0, -30deg );
        -webkit-transform: skew( 0, -30deg );
}

#horizontal {
    margin: 55px 0 0 0;
    transform:             skew( -30deg, 0 );
        -ms-transform:     skew( -30deg, 0 );
        -moz-transform:    skew( -30deg, 0 );
        -o-transform:      skew( -30deg, 0 );
        -webkit-transform: skew( -30deg, 0 );
}

.image {
    background-image: url( 'http://placekitten./100' );  
    border: 1px solid black;
    display: inline-block;
    height: 100px;
    margin-right: 20px; 
    vertical-align: top;
    width: 100px;  
}

HTML

<div id="original" class="image"></div>
<div id="vertical" class="image"></div>
<div id="horizontal" class="image"></div>

I found the solution: http://www.w3schools./html5/playcanvas.asp?filename=playcanvas_transformb&preval=1,0,0,1,0,0

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far