我想添加一個畫布在另一個畫布上 - 我如何讓這個函數等待開始,直到第一個畫布創建?使函數等待,直到元素存在
function PaintObject(brush) {
this.started = false;
// get handle of the main canvas, as a DOM object, not as a jQuery Object. Context is unfortunately not yet
// available in jquery canvas wrapper object.
var mainCanvas = $("#" + brush).get(0);
// Check if everything is ok
if (!mainCanvas) {alert("canvas undefined, does not seem to be supported by your browser");}
if (!mainCanvas.getContext) {alert('Error: canvas.getContext() undefined !');}
// Get the context for drawing in the canvas
var mainContext = mainCanvas.getContext('2d');
if (!mainContext) {alert("could not get the context for the main canvas");}
this.getMainCanvas = function() {
return mainCanvas;
}
this.getMainContext = function() {
return mainContext;
}
// Prepare a second canvas on top of the previous one, kind of second "layer" that we will use
// in order to draw elastic objects like a line, a rectangle or an ellipse we adjust using the mouse
// and that follows mouse movements
var frontCanvas = document.createElement('canvas');
frontCanvas.id = 'canvasFront';
// Add the temporary canvas as a second child of the mainCanvas parent.
mainCanvas.parentNode.appendChild(frontCanvas);
if (!frontCanvas) {
alert("frontCanvas null");
}
if (!frontCanvas.getContext) {
alert('Error: no frontCanvas.getContext!');
}
var frontContext = frontCanvas.getContext('2d');
if (!frontContext) {
alert("no TempContext null");
}
this.getFrontCanvas = function() {
return frontCanvas;
}
this.getFrontContext = function() {
return frontContext;
}
當您單擊創建畫布時,運行該函數或觸發一個運行該函數的處理程序的事件。當元素變得可用時,沒有內置的跨瀏覽器事件。 – 2013-04-22 14:18:44
可能重複的[如何等到一個元素存在?](http://stackoverflow.com/questions/5525071/how-to-wait-until-an-element-exists) – user2284570 2014-10-12 19:59:33