how to set size of canvas to its background image?

Ali Elgazar

i have the following CSS code for the canvas object which i have

canvas{
    border: solid black 2px;
    background:url("http://upload.wikimedia.org/wikipedia/en/a/aa/Bart_Simpson_200px.png");
    background-repeat:no-repeat;
    }

how do i make the size of the canvas equal to the size of that background image?

joym8

If this is a single unchanging image, use the width and height properties in CSS.

canvas{
    border: solid black 2px;
    background: url("http://upload.wikimedia.org/wikipedia/en/a/aa/Bart_Simpson_200px.png");
    background-repeat:no-repeat;
    width: 200px;
    height: 298px;
}

If this is a dynamic image, use JavaScript.

HTML:

<canvas id="canvas"><img src="dynamic_url" id="dynamic_img" /></canvas>

JS:

function resizeCanvas()
{
    var img = document.getElementById('dynamic_img'); 
    var width = img.clientWidth;
    var height = img.clientHeight;

    var canvas = document.getElementById('canvas'); 
    canvas.style.width = width;
    canvas.style.height = height;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to set size of canvas to its background image?

From Dev

How to set the background image to canvas

From Dev

Set size of the div to its background image

From Dev

Set size of the div to its background image

From Dev

How to save an image in its original size in a canvas after scaled the canvas

From Dev

How do I set background image to a Canvas

From Dev

How to Set wallpaper of image with its actual size?

From Dev

How to set the background image according to screen size?

From Dev

How to set button background image size

From Dev

How to set the background image of FabricJS canvas to image from a web service?

From Dev

Set size of image from canvas

From Dev

Set size of image from canvas

From Dev

How to set repeating background image for a canvas in Fabric JS?

From Dev

how to resize an image or done as a NSAttributedString NSTextAttachment (or set its initital size)

From Dev

How do I set the background image size of a table cell in swift?

From Dev

How do I set the background image size in CSS?

From Dev

How to set a big size image background in Android Studio

From Dev

Scaling a background image relative to its own size?

From Dev

How to set background image?

From Dev

How To Set Background Image

From Dev

Transition background image size only, in css? (When background size is not set)

From Dev

Set a size to an Image on Android and keep its quality

From Dev

How can I set an image as a background for a div and set its "ZOrder" to 0?

From Dev

Unable to set size in percent on multiple image background

From Dev

retrieve the size of a background image set to "cover"

From Dev

Set background image to cover according to viewport size

From Dev

How to make adaptive background image increase and returns to its original size constantly?

From Dev

Can i set the uploaded image as a background image of canvas?

From Dev

How to draw a line in canvas with a background image

Related Related

  1. 1

    how to set size of canvas to its background image?

  2. 2

    How to set the background image to canvas

  3. 3

    Set size of the div to its background image

  4. 4

    Set size of the div to its background image

  5. 5

    How to save an image in its original size in a canvas after scaled the canvas

  6. 6

    How do I set background image to a Canvas

  7. 7

    How to Set wallpaper of image with its actual size?

  8. 8

    How to set the background image according to screen size?

  9. 9

    How to set button background image size

  10. 10

    How to set the background image of FabricJS canvas to image from a web service?

  11. 11

    Set size of image from canvas

  12. 12

    Set size of image from canvas

  13. 13

    How to set repeating background image for a canvas in Fabric JS?

  14. 14

    how to resize an image or done as a NSAttributedString NSTextAttachment (or set its initital size)

  15. 15

    How do I set the background image size of a table cell in swift?

  16. 16

    How do I set the background image size in CSS?

  17. 17

    How to set a big size image background in Android Studio

  18. 18

    Scaling a background image relative to its own size?

  19. 19

    How to set background image?

  20. 20

    How To Set Background Image

  21. 21

    Transition background image size only, in css? (When background size is not set)

  22. 22

    Set a size to an Image on Android and keep its quality

  23. 23

    How can I set an image as a background for a div and set its "ZOrder" to 0?

  24. 24

    Unable to set size in percent on multiple image background

  25. 25

    retrieve the size of a background image set to "cover"

  26. 26

    Set background image to cover according to viewport size

  27. 27

    How to make adaptive background image increase and returns to its original size constantly?

  28. 28

    Can i set the uploaded image as a background image of canvas?

  29. 29

    How to draw a line in canvas with a background image

HotTag

Archive