One of my interface elements is rendered using an HTML5 element <canvas>and the corresponding JavaScript API. This item is used in several places on the same screen and on multiple screens in the application. What is the most efficient way to display this wherever it is needed?
My first idea is to draw a master on canvas, which I then clone and paste where necessary on the page. A master canvas might look something like this:
var master = $('<canvas>').attr({
width: 100,
height: 100
}),
c = master[0],
ctx = c.getContext("2d");
ctx.fillStyle = "#FF0000";
ctx.fillRect(0, 0, 150, 75);
Let's say I want to duplicate the canvas in these containers div:
<div class="square-container" id="square_header"></div>
...
<div class="square-container" id="square_dataTable"></div>
...
<div class="square-container" id="square_gallery"></div>
....
When the page loads, I will do this to insert a duplicate canvas element into each container:
$(document).ready(function() {
$('.square-container').each(function() {
master.clone().appendTo($(this));
});
});
, , , , , . , , , .
, , toDataURL() :
var master = $('<canvas>').attr({
width: 100,
height: 100
}),
c = master[0],
ctx = c.getContext("2d");
ctx.fillStyle = "#FF0000";
ctx.fillRect(0,0,150,75);
var square = c.toDataURL('image/png');
, :
<img src="" id="square_header" class="square" alt="" />
...
<img src="" id="square_dataTable1" class="square" alt="" />
...
<img src="" id="square_gallery" class="square" alt="" />
....
SRC :
$(document).ready(function() {
$('img.square').attr('src', square);
});
, . , , ? , <canvas>, , , ?
, , javascript ( , ) CANVAS_ELEMENT.toDataURL() cookie, ?