Processing.js 无法在内部绘制 canvas
Processing.js unable to draw inside inner canvas
如果我将 canvas1 移到 holderCanvas 之外,下面的代码可以正常工作。但是,我在这种安排中需要它,因为 holderCanvas 正在实现缩放和平移功能。
<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>
里面的内容是回退的,只有在浏览器不支持<canvas>
的时候才显示。这就是 canvas1
不会显示在这里的原因。
如果我将 canvas1 移到 holderCanvas 之外,下面的代码可以正常工作。但是,我在这种安排中需要它,因为 holderCanvas 正在实现缩放和平移功能。
<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>
里面的内容是回退的,只有在浏览器不支持<canvas>
的时候才显示。这就是 canvas1
不会显示在这里的原因。