Whisk and Canvas

A JS beginner's question. The following does not run in Whisk. It does run in preview on BBEdit and Textmate and on a browser. Does Whisk not recognize the canvas or have I set up this simple sample incorrectly?

<!DOCTYPE html>
<html lang="english">

<head>
    <title>Canvas Test</title>
</head>

<body>

    <canvas id=myCanvas height=800 width=800;">
    </canvas>

    <script>
        myCanvas.onclick = function() {

            var Questions = ["How much is (16)^(1/2)? 8, 4, 2, 32",
                "Which species was first in space? Dog, Cat, Chimp, Rat",
                "Who has the largest number, just type first letter: Avogadro, Eddington, Graham, or Planck?",
            ];
            var Answers = ["4", "Dog", "G"];
            var which_one = Math.floor(Math.random() * Questions.length);
            var answer = prompt(Questions[which_one]);

            if (answer == Answers[which_one]) alert("You are correct")
            else alert("Oops, not right");

        }
    </script>
</body>

</html>

I don't think any browser would display anything for this code. You're going to want this to setup the Canvas element:

<canvas id="myCanvas" height="800" width="800"></canvas>

Here's more on the canvas element: https://www.w3schools.com/html/html5_canvas.asp
It requires quotes around the canvas element's properties. Was this copied from somewhere?

Thanks for your reply, Daniel. I added quotation marks and color for visibility. Still does not run in preview on Whisk (or VS Code). Curiously, even without the quotation marks it will run in Safari, BBEdit, Nova, Textmate, even on a w3schools try-it page. All run with or without the quotation marks.

Maybe something about the way Whisk (and VS Code, although that’s outside the scope of discussion) treats the canvas?

<!DOCTYPE html>
<html lang="english">

<head>
    <title>Canvas Test</title>
</head>

<body>


   <canvas id="myCanvas" height="200" width="200" style="background-color: green"></canvas>

    <script>
        myCanvas.onclick = function() {

            var Questions = ["How much is (16)^(1/2)? 8, 4, 2, 32",
                "Which species was first in space? Dog, Cat, Chimp, Rat",
                "Who has the largest number, just type first letter: Avogadro, Eddington, Graham, or Planck?",
            ];
            var Answers = ["4", "Dog", "G"];
            var which_one = Math.floor(Math.random() * Questions.length);
            var answer = prompt(Questions[which_one]);

            if (answer.toUpperCase() == Answers[which_one]) 
            alert("You are right !")
            else alert("Sorry, not right");

        }
    </script>
</body>

</html>

You don't see the green square?

If not, can you select Help > Report an issue and we can take a look?

Yes, certainly I see the green square of the canvas. But the quiz will not play in Whisk preview. It will play in the preview of other editors, including w3schools. It will play on a browser. It only does not play in the previews of Whisk (or VS Code). Is Whisk preview live?

It was unclear what you were saying wasn't working -- what isn't working is the actual 'click' handler when clicking on the canvas element.

For now I recommend either using a non-canvas element or clicking preview to load this in a browser. This seems like a bug.

Button doesn't work either in preview.

<!DOCTYPE html>
<html lang="english">

<head>
    <title>test</title>
</head>

<body>
<button type="button" onclick="alert('Hello world!')">Click Me!</button>
</body>

</html>

That's true -- for testing, you could add a console.log() if you wanted some visual feedback instead of an alert. This would show up in the console which you can get to by clicking i in the top toolbar. Clicks like this will be detected:

<!DOCTYPE html>
<html>
<body>

<p>This example uses the HTML DOM to assign an "onclick" event to a p element.</p>

<p id="demo">Click me.</p>

<script>
document.getElementById("demo").onclick = function() {myFunction()};

function myFunction() {
  document.getElementById("demo").innerHTML = "YOU CLICKED ME!";
}
</script>

</body>
</html>

I suppose, but it seems more intuitive to click a button rather than clicking text acting as a button and writing code to accommodate the editor. Hopefully Team Tumult will fix the live preview for canvas, buttons, and so on.

Thanks, Daniel.

1 Like

Thanks for the feedback. Please note that the underlying issues is that Whisk isn't displaying alerts/prompts - the actual clicks work fine. We just need to implement some callbacks for these; I don't think it will be too difficult.

Is there an update? Much appreciated.

Sorry, there have not been any updates addressing this issue yet.