We can use canvas for drawing custom shapes. I need to draw my shape dynamically as a canvas item and place it for a div background item. My pages generates run time and they aren't static html code so i can't use tricky methods. What's your idea?
Regards
You can use a canvas element as a background, but CSS doesn't have anything to do with that.
To add a scrollable canvas inside a div, we can make the div scrollable and put a canvas inside it. to add a div with the overflow CSS property set to auto . And then we add a canvas inside it with a red border.
Say you want to put an image or two on a webpage. One way is to use the background-image CSS property. This property applies one or more background images to an element, like a <div> , as the documentation explains.
Looks like you searching for toDataURL().
UPD: Here a usage exaple:
dataUrl = your_canvas.toDataURL();
your_div.style.background='url('+dataUrl+')'
Live demo on jsFiddle
Sounds like you need canvas2image: https://github.com/hongru/canvas2image
You can create a canvas and then get the contents as a png:
var canvas = document.createElement("canvas");
....do stuff here...
var img = Canvas2Image.convertToPNG(canvas, canvas.width, canvas.height);
Then all you need to do is use the png as a background image:
document.body.style.background = "url(" + img.src + ")";
Please correct me if any of this is wrong.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With