I'm making a Twine-based comic using HTML5 Canvas elements instead of images. The Canvas elements contain an image with a textbox on it. Later, I'll align this textbox with the speech bubble so I can alter the speech bubble contents dynamically using a custom <<panel>> widget. However, when I use the widget, nothing shows up.
WIDGET CODE
<<widget "panel">>
/* convenience variables! */
<<set _imgurl to $args[0]>>
<<set _balloontext to $args[1]>>
/* if there is just an image URL arg and no balloon text arg...*/
<<if _imgurl && !_balloontext>>
[img[$img + _imgurl]
<</if>>
/* if there is both... */
<<if _imgurl && _balloontext>>
<<script>>
$(output).wiki("This text appears right now.");
//create image but do not add it to page
var img = document.createElement("img");
$(output).wiki("This text does not appear!! Why?");
var canvas = document.createElement("canvas");
var imgUrl = State.variables[img]+State.temporary[imgurl];
img.src = imgUrl;
//add canvas to DOM
var story = document.getElementById("story");
story.appendChild(canvas);
ctx = canvas.getContext('2d');
canvas.width = img.width();
canvas.height = img.height();
ctx.drawImage(img, 0, 0);
ctx.font = "36pt Verdana";
//redraw image
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.drawImage(img, 0, 0);
//refill text
ctx.fillStyle = "red";
ctx.fillText(State.temporary[balloontext],40,80);
<</script>>
<</if>>
<</widget>>
PASSAGE CODE
<<panel "panel0.png" "Hello!">>
(Note: $img is a global variable referring to the path I'm using for my images. The following Passage input creates an image successfully:)
[img[$img + "rosequartz.png"][Link]]
Edit:
Thanks to the answers below, this code worked:
window.panel = function(imgUrl, balloonText, link) {
//if given both image url and balloon text
if (imgUrl != null && balloonText != null) {
/*create image and canvas*/
var img = document.createElement('img');
var canvas = document.createElement('canvas');
img.onload = function () {
/*add canvas and image to DOM*/
var story = document.getElementById("story");
story.appendChild(img);
story.appendChild(canvas);
/*My image is 2560 x 960 pixels*/
canvas.width = img.naturalWidth/2;
canvas.height = img.naturalHeight/2;
var ctx = canvas.getContext('2d');
ctx.strokeStyle="#FF0000";
ctx.strokeRect(0,0,canvas.width,canvas.height);
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
/*fill text*/
ctx.font = "36pt Comic Sans MS";
ctx.textBaseline = "top";
ctx.fillStyle = "black";
ctx.fillText(balloonText,620,100);
$(story.content).wiki(canvas.height);
}
img.src = State.variables.img + imgUrl;
}
//if given only an image url
else if (imgUrl != null && balloonText == null) {
$(story.content).wiki("[img[$img + _imgurl]]");
}
}
<<run panel("panel0.png", "Hello World!");>>
However, this caused major performance issues (scrolling lags a LOT with my trackpad) so I'll try using SVGs instead.