Processing.js unable to draw inside inner canvas

44 views Asked by At

The code below works fine if I move out the canvas1 outside holderCanvas. However, I need it in this arrangement because, the holderCanvas is implementing a zoom and pan feature.

<html>
<head>
  <script src="processing.min.js"></script>
</head>
<body><h1>Processing.js</h1>
<h2>Simple processing.js via JavaScript</h2>
<p>Clock</p>

<canvas id="holderCanvas" width="200" height="200" style="opacity:0.1;background-color:red;">
  <canvas id="canvas1" width="200" height="200"></canvas>
</canvas>

<script id="script1" type="text/javascript">

// Simple way to attach js code to the canvas is by using a function
function sketchProc(processing) {
  // Override draw function, by default it will be called 60 times per second
  processing.draw = function() {
    // determine center and max clock arm length
    var centerX = processing.width / 2, centerY = processing.height / 2;
    var maxArmLength = Math.min(centerX, centerY);

    function drawArm(position, lengthScale, weight) {      
      processing.strokeWeight(weight);
      processing.line(centerX, centerY, 
        centerX + Math.sin(position * 2 * Math.PI) * lengthScale * maxArmLength,
        centerY - Math.cos(position * 2 * Math.PI) * lengthScale * maxArmLength);
    }

    // erase background
    processing.background(224);

    var now = new Date();

    // Moving hours arm by small increments
    var hoursPosition = (now.getHours() % 12 + now.getMinutes() / 60) / 12;
    drawArm(hoursPosition, 0.5, 5);

    // Moving minutes arm by small increments
    var minutesPosition = (now.getMinutes() + now.getSeconds() / 60) / 60;
    drawArm(minutesPosition, 0.80, 3);

    // Moving hour arm by second increments
    var secondsPosition = now.getSeconds() / 60;
    drawArm(secondsPosition, 0.90, 1);
  };

}

var canvas = document.getElementById("canvas1");
var p = new Processing(canvas, sketchProc);
</script>
</body>
</html>

I can do all the drawing in the outer canvas. However, to maintain modular components, I am implementing this design. Please explain if this is a wrong way.

1

There are 1 answers

0
damio On BEST ANSWER

The content inside <canvas> is for the fallback, it only display if <canvas> is not supported by the browser. That's why canvas1 won't display here.