gradientview.lzx
<library>
<include href="extensions/drawview.lzx"/>
<class name="gradientview" extends="drawview">
<attribute name="colorFrom" type="color"/>
<attribute name="colorTo" type="color"/>
<attribute name="borderSize" type="number" value="0
"/>
<attribute name="borderSizeTop" type="number" value="0
"/>
<attribute name="borderSizeBottom" type="number" value="0
"/>
<attribute name="borderSizeLeft" type="number" value="0
"/>
<attribute name="borderSizeRight" type="number" value="0
"/>
<attribute name="borderColor" type="color" value="0x000000
"/>
<attribute name="borderColorTop" type="color"/>
<attribute name="borderColorBottom" type="color"/>
<attribute name="borderColorLeft" type="color"/>
<attribute name="borderColorRight" type="color"/>
<attribute name="gradientType" type="string" value="linear
"/>
<attribute name="gradientOrientation" type="string" value="vertical
"/>
<attribute name="stretchHeight" type="number"/>
<attribute name="stretchWidth" type="number"/>
<handler name="onheight">
this.setAttribute('stretchHeight',this.height);
this.draw();
</handler>
<handler name="onwidth">
this.setAttribute('stretchWidth',this.width);
this.draw();
</handler>
<handler name="oncontext" method="draw"/>
<method name="draw" args="ignore=null">
if (! this.context) return;
this.clear();
if (this.stretchHeight == null) {
this.stretchHeight = this.height;
if (this.measureHeight() > this.height) { this.stretchHeight = this.measureHeight(); }
}
if (this.stretchWidth == null) {
this.stretchWidth = this.width;
if (this.measureWidth() > this.width) { this.stretchWidth = this.measureWidth(); }
}
// define box for gradient
this.beginPath();
this.rect(0, 0, this.stretchWidth, this.stretchHeight);
this.closePath();
// create gradient color
var g;
if (this.gradientType == 'linear') {
if (this.gradientOrientation == 'vertical') {
g = this.createLinearGradient(0,0,0,this.stretchHeight);
}
if (this.gradientOrientation == 'horizontal') {
g = this.createLinearGradient(0,this.stretchHeight,this.stretchWidth,this.stretchHeight);
}
if (this.gradientOrientation == 'diagonalleft') {
g = this.createLinearGradient(0,0,this.stretchWidth,this.stretchHeight);
}
if (this.gradientOrientation == 'diagonalright') {
g = this.createLinearGradient(this.stretchWidth,0,0,this.stretchHeight);
}
}
if (this.gradientType == 'radial') {
g = this.createRadialGradient(0-(this.stretchWidth*.5),-(this.stretchHeight*.5),0,this.stretchWidth*1.5,this.stretchHeight*1.5,0);
}
if ((g != null) && (this.colorFrom != null)) {
this.globalAlpha = 1;
g.addColorStop(0, this.colorFrom);
g.addColorStop(1, this.colorTo != null ? this.colorTo : this.colorFrom);
this.fillStyle = g;
this.fill();
}
if ((this.borderSize > 0) || (this.borderSizeTop > 0)) {
if (this['borderColorTop'] == null) {
this.borderColorTop = this.borderColor;
}
// create top border
this.beginPath();
var borderWidth = this.borderSizeTop ? this.borderSizeTop : this.borderSize;
var borderOffset = borderWidth - 1;
this.moveTo(borderOffset,borderOffset);
this.lineTo(this.stretchWidth,borderOffset);
this.strokeStyle = this.borderColorTop;
this.lineWidth = borderWidth;
this.stroke();
this.closePath();
}
if ((this.borderSize > 0) || (this.borderSizeBottom > 0)) {
if (this['borderColorBottom'] == null) {
this.borderColorBottom = this.borderColor;
}
// create bottom border
this.beginPath();
var borderWidth = this.borderSizeBottom ? this.borderSizeBottom : this.borderSize;
var borderOffset = borderWidth - 1;
this.moveTo(borderOffset,this.stretchHeight-1);
this.lineTo(this.stretchWidth,this.stretchHeight-1);
this.strokeStyle = this.borderColorBottom;
this.lineWidth = borderWidth;
this.stroke();
this.closePath();
}
if ((this.borderSize > 0) || (this.borderSizeLeft > 0)) {
if (this['borderColorLeft'] == null) {
this.borderColorLeft= this.borderColor;
}
// create left border
this.beginPath();
var borderWidth = this.borderSizeLeft ? this.borderSizeLeft : this.borderSize;
var borderOffset = borderWidth - 1;
this.moveTo(borderOffset,borderOffset);
this.lineTo(borderOffset,this.stretchHeight-borderOffset);
this.strokeStyle = this.borderColorLeft;
this.lineWidth = borderWidth;
this.stroke();
this.closePath();
}
if ((this.borderSize > 0) || (this.borderSizeRight > 0)) {
if (this.borderColorRight == null) {
this.borderColorRight= this.borderColor;
}
// create right border
this.beginPath();
var borderWidth = this.borderSizeRight ? this.borderSizeRight : this.borderSize;
var borderOffset = borderWidth - 1;
this.moveTo(this.stretchWidth-1,borderOffset);
this.lineTo(this.stretchWidth-1,this.stretchHeight-1);
this.strokeStyle = this.borderColorRight;
this.lineWidth = borderWidth;
this.stroke();
this.closePath();
}
</method>
</class>
</library>
Cross References
Includes
Classes