S
S
Sedbol2020-01-10 14:05:56
JavaScript
Sedbol, 2020-01-10 14:05:56

How to draw elements on canvas?

How to draw elements on canvas? The text from the div block is rendered. And how to display a tag in a place with text?
If I insert a tag it is not drawn on the canvas.
So there is only text

let canvas = document.getElementById("canvas");
    let ctx = canvas.getContext("2d");
        let data = "data:image/svg+xml,"+
            "<svg xmlns='http://www.w3.org/2000/svg' width='200' height='200'>" +
            "<foreignObject width='100%' height='100%' >" +
            "<div xmlns='http://www.w3.org/1999/xhtml'>Текст</div>" +
            "</foreignObject>" +
            "</svg>";
        let img = new Image();
        img.src = data;
        img.onload = function() { ctx.drawImage(img, 0, 0); }
        console.info(data)

And if so, then there is nothing. How to insert an img tag into a drawing in place with text?
let canvas = document.getElementById("canvas");
    let ctx = canvas.getContext("2d");
        let data = "data:image/svg+xml,"+
            "<svg xmlns='http://www.w3.org/2000/svg' width='200' height='200'>" +
            "<foreignObject width='100%' height='100%' >" +
            "<div xmlns='http://www.w3.org/1999/xhtml'>Текст</div><img src='coin.jpg'>" +
            "</foreignObject>" +
            "</svg>";
        let img = new Image();
        img.src = data;
        img.onload = function() { ctx.drawImage(img, 0, 0); }
        console.info(data)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Belyaev, 2020-01-10
@Sedbol

The problem is src='coin.jpg', the image does not know how to load external resources, use the same data url for the embedded image

A
Alexander, 2020-01-10
@Seasle

html2canvas

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question