processing.js无法在内部画布中绘制



如果我将 canvas1 移出 HolderCanvas ,则下面的代码正常工作。但是,我在这种安排中需要它,因为HolderCanvas正在实现Zoom和Pan功能。

<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>

我可以在外部画布中完成所有图纸。但是,为了维护模块化组件,我正在实施此设计。请说明这是错误的。

<canvas>中的内容用于后备,仅当浏览器不支持<canvas>时才显示。这就是为什么canvas1不会在此处显示。

最新更新