I am writing a program that embeds text in canvases, each canvas contains one character (Kanji) that will be animated, drawn one stroke at a time.
I have encountered a few problems so far:
I might have overcome the second issue by inserting the text inside the <canvas>
tag (though I have no way to check if it works, as I can't select the canvases to copy the content); but I have no idea on how to solve the first issue.
So the question is: how can I make my canvases behave as images with alt
attributes, so that users can copy them as if they were normal characters?
JsFiddle example with <img alt="...">
Thanks!
I have written a few more tests:
div
with canvas
+ absolutely positioned img
Issues: adds spaces between div
elements (tested on Firefox), selection sometimes flickers.
canvas
+ partially hidden span
Issues: selection completely hides the canvas.
I've also tried with labels, but there were even more issues and no noticeable advantage.
Decorative images are images that do not add important information. These are often used to 'pretty up' the page and add ambiance. They should not have alt-text associated with them (alt=”” designates a null/empty value) and when possible should be marked as decorative.
There is no such alt
attribute to canvas element.
From "MDN - Basic Usage of canvas":
At first sight a
<canvas>
looks like the<img>
element, with the only clear difference being that it doesn't have the src and alt attributes.
Also, the innerHTML of the canvas will only be accessible to browsers which don't support <canvas>
element.
What you could do however, if you only draw once on the canvas and if there is no Cross-Origin involved in those drawings (i.e external images), is to create new <img>
tags with their src
set to the canvas.toDataURL()
, and assign those an alt attribute.
var img = new Image();
img.onload = function(){
canvas.parentNode.replaceChild(this, canvas);
this.alt = 'your alt content';
}
img.src = canvas.toDataURL();
Or alternatively, for your particular case, you could also append a <span>
element with its opacity set to 0 , its height/width to 1px and positionning it absolutely.
It should not be visible, but you would be able to copy paste its content.
body {
font-size: 14px;
}
img {
display: inline-block;
vertical-align: baseline;
cursor: text;
height: 1em;
}
.canvas_alt {
position: absolute;
max-height: 1px;
max-width: 1px;
opacity: 0
}
canvas {
border: 1px solid
}
Copy the images as if text:
<br />
<br />X<canvas height="50" width="50"></canvas><span class="canvas_alt">alt text</span>Y
<br />
<br />Try pasting it here, is should be 'XiiY':
<br />
<input />
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