Cutting an Image into pieces through Javascript

后端 未结 3 2118
粉色の甜心
粉色の甜心 2020-11-30 00:52

I am creating a simple Jigsaw puzzle. In order to do this, I need to cut the picture I am using into 20 pieces. Is there a way in Javascript to cut a picture into 20 equal p

3条回答
  •  醉话见心
    2020-11-30 01:17

    You can do this by setting the image as a background on a div, then setting its background-position. This is basically the same as using CSS Sprites.

    (assume pieces are 100 x 100px)

    CSS:

    .puzzle {
       background-image:url(/images/puzzle.jpg);
       width:100px;
       height:100px;
    }
    
    .piece1 {
       background-position:0 0
    }
    
    .piece2 {
       background-position:-100px -100px
    }
    

提交回复
热议问题