I’m having a similar problem with trying to set a widget background image with an image rendered from a DrawContext.
Here’s the simplified script I’m working with:
let widget = new ListWidget()
widget.setPadding(0, 0, 0, 0);
widget.backgroundColor = new Color("#4a525a")
// Traditional widget text -- works
// let titleTxt = widget.addText('Hello world')
// titleTxt.font = Font.boldSystemFont(24)
// titleTxt.textColor = new Color("#eeeeee") // almost white
// End test drawing
// Drawing in a graphics context
let dc = new DrawContext();
dc.size = new Size(282, 282);
dc.opaque = true; // true = solid white; false = solid widget background color
dc.setFont(Font.boldSystemFont(24));
dc.setTextColor(new Color('#eeeeee'));
dc.drawText('Hello draw context', new Point(30, 30));
widget.backgroundImage = dc.getImage();
// End DC drawing
// Present widget, or queue for Home screen
if ( ! config.runsInWidget) {
await widget.presentSmall()
}
Script.setWidget(widget)
Script.complete()
When I set DrawContext.opaque
to true, the widget renders solid white. When I set it to false, I get the #4a525a
gray that’s set as the widget.backgroundColor
. In neither case is the text rendered.
Looking at the DrawContext docs, there’s mention of a call to “finish” the drawing, endDrawing()
:
When you are done drawing your image, you should call endDrawing().
It’s mentioned in several places in the DrawContext docs. However, there’s no doc for that function, and when I add it to the script, I get an error that Exception Occurred, endDrawing()
is not a function, is undefined.
I have scripts from other folks that use DrawContext, and which seem to make it work. But there’s some magic sauce I’m not seeing in their code, and even when I reduce it to limited amounts of drawing, it still works. But I can’t figure out why those work, but my simple code does not.