1

Since I am still learning the basics of JavaScript, can someone suggest me a possible way to solve the error of this Scriptable code? (I am trying to draw a simple rectangle at the centre of the widget).

Please, I don’t want to write the solution code, but to explain me, if possible, what I have done wrong and how can I do it better, after all, as I said, I am still learning the new language.

CODE:

async function createCentralRect(w, h) {
  let newRect = new Rect(0, 0, w, h)
  return newRect
}

async function createWidget() {
  let widget = new ListWidget()

  let rect = await createCentralRect(2, 2)
  let cnv = new DrawContext()
  let rectColor = new Color("#000")
  cnv.fillRect(rect)
  cnv.setFillColor(rectColor)
  return widget
}

let widget = await createWidget()
if (config.runsInWidget) {
  Script.setWidget(widget)
} else {
  widget.presentSmall()
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Using your browser's devtools are you seeing any errors? – A Haworth Apr 23 '22 at 05:09
  • None. I run the code. The widget is show entirely white, with no black rectangle (more of a square with (2,2) as argument) in it. But I am not using browser, but the apple application “Scriptable” –  Apr 23 '22 at 05:12
  • I don't know Scriptable, but I don't see anything that connects `widget` and `cnv`. – Barmar Apr 23 '22 at 07:24

0 Answers0