我在5.0中运行此代码时遇到问题,但它在3.3中工作正常。 我已经设定了所有的东西,但它仍然给出了问题。我这样做是错误的吗?
<canvas width="1000" height="584">
<drawview width="200" height="300"
x="12"
y="12">
<handler name="onwidth">
this.redraw();
</handler>
<handler name="onheight">
this.redraw();
</handler>
<method name="redraw">
this.clear();
var roundness = 5;
this.beginPath();
this.moveTo(roundness, 0);
this.lineTo(this.width - roundness, 0);
this.quadraticCurveTo(this.width, 0, this.width, roundness);
this.lineTo(this.width, this.height - roundness);
this.quadraticCurveTo(this.width, this.height, this.width - roundness, this.height);
this.lineTo(roundness, this.height);
this.quadraticCurveTo(0, this.height, 0, this.height - roundness);
this.lineTo(0, roundness);
this.quadraticCurveTo(0, 0, roundness, 0);
this.closePath();
var g = this.createRadialGradient(-this.width * .5, -this.height *.5, .7, 1.5 * this.width, 1.5 * this.height, 0)
this.globalAlpha = 0;
g.addColorStop(0, 0x000000);
this.globalAlpha = 0.8;
g.addColorStop(1, 0xffffff);
this.setAttribute("fillStyle", g);
this.fill();
</method>
</drawview>
</canvas>
答案 0 :(得分:1)
在初始化drawview期间,在组件初始化尚未完成的时间点调用onwidth和onheight处理程序。如果检查组件的isinited属性,则可以确保仅在组件准备好时调用重绘方法。我在此示例中添加了一些调试输出,以向您显示正在发生的事情:
<drawview id="dv" width="100%" height="100%"
x="12"
y="12">
<attribute name="isready" value="false" type="boolean" />
<handler name="oncontext">
this.setAttribute("isready", true);
this.redraw();
</handler>
<handler name="onwidth">
Debug.write('onwidth: calling redraw()');
this.redraw();
</handler>
<handler name="onheight">
Debug.write('onheight: calling redraw()');
this.redraw();
</handler>
<method name="redraw">
Debug.info('redraw: this.ininited=' + this.isinited + ' / isready=' + this.isready);
if (this.isready) {
this.clear();
var roundness = 5;
this.beginPath();
this.moveTo(roundness, 0);
this.lineTo(this.width - roundness, 0);
this.quadraticCurveTo(this.width, 0, this.width, roundness);
this.lineTo(this.width, this.height - roundness);
this.quadraticCurveTo(this.width, this.height, this.width - roundness, this.height);
this.lineTo(roundness, this.height);
this.quadraticCurveTo(0, this.height, 0, this.height - roundness);
this.lineTo(0, roundness);
this.quadraticCurveTo(0, 0, roundness, 0);
this.closePath();
var g = this.createRadialGradient(-this.width * .5, -this.height *.5, .7, 1.5 * this.width, 1.5 * this.height, 0);
this.globalAlpha = 0;
g.addColorStop(0, 0x000000);
this.globalAlpha = 0.8;
g.addColorStop(1, 0xffffff);
this.setAttribute("fillStyle", g);
this.fill();
}
</method>
</drawview>
编辑:更新了代码支持以使用oncontext事件而不是isinited值
我查看了drawview文档,并且drawview有一个名为oncontext事件的特殊事件。来自文档:
当上下文准备好使用时,会发送oncontext事件 在IE DHTML中可能需要一些时间。
如果您查看OpenLaszlo参考中的drawview文档,您将看到这些示例使用oncontext事件处理程序绘制到画布中。
我已更新我的解决方案以使用其他属性 isready ,一旦收到oncontext事件,该属性设置为true。正如您在下面的调试输出中所看到的,isinited在isready设置为true之前设置为true:
onwidth: calling redraw()
INFO: redraw: this.ininited=true / isready=false
onheight: calling redraw()
INFO: redraw: this.ininited=true / isready=false
INFO: redraw: this.ininited=true / isready=true
如果为DHTML运行时编译错误的代码,您应该看到以下警告:
警告:尚未定义this.context。请检查 在使用绘图方法之前存在context属性,和/或 注册oncontext事件以找出属性的时间 可用。
不幸的是,SWF运行时没有显示这样的警告。