如何用AS做个跟windows画图工具一样的程序

2005-11-28 10:27:33

[code] //画笔的样式 var penStyle = "line"; //是否正在画 var drawing = false; //绘图的起始位置 var startX, startY; //绘图的终点位置 var endX, endY; //数据指针,预设为指向空 var dataPointer = -1; //建立存放数据的XML对象 var dataOfDraw = new XML(); //设定圆钮的事件控制函数 penStyleGroup.setChangeHandler("onPenChange"); function onPenChange(component) { penStyle = component.getData(); } //一开始设定Undo和Redo按钮为失效 undo_btn.enabled = false; redo_btn.enabled = false; //底层画布 _root.cr&#101;ateEmptyMovieClip("canvas1", 1); //暂时画布 _root.cr&#101;ateEmptyMovieClip("canvas2", 2); //建立遮罩对象 _root.attachMovie("m_canvasMask", "mask1", 3); _root.attachMovie("m_canvasMask", "mask2", 4); //调整画布的大小及位置 for (i=1; i<=2; i++) { with (eval("mask"+i)) { _x = canvas_btn._x; _y = canvas_btn._y; _width = canvas_btn._width; _height = canvas_btn._height; } } //设定遮罩 canvas1.setMask(mask1); canvas2.setMask(mask2); // canvas_btn.onPress = function() { drawing = true; startX = _xmouse; startY = _ymouse; endX = startX; endY = startY; }; canvas_btn.onRelease = function() { drawing = false; canvas2.clear(); //判断图形是否有被画出来 if (startX-endX == 0 && startY-endY == 0) { return; } //绘在底层画布 drawOnCanvas(canvas1, penStyle, penSize_mc.value, penColor_mc.getRGB(), fullColor_mc.getRGB()); //清除可以Redo的数据 for (i=dataOfDraw.childNodes.length-1; i>dataPointer; i--) { dataOfDraw.childNodes[i].removeNode(); } //设定Redo为无效 redo_btn.enabled = false; //加入数据 addDataOfDraw(); }; //放开按钮和在感应区外放开按钮呼叫的方法相同 canvas_btn.onReleaseOutside = canvas_btn.onRelease; //加入一项数据 function addDataOfDraw() { var aDraw = dataOfDraw.cr&#101;ateElement(penStyle); aDraw.attributes.sx = startX; aDraw.attributes.sy = startY; aDraw.attributes.ex = endX; aDraw.attributes.ey = endY; aDraw.attributes.ps = penSize_mc.value; aDraw.attributes.pc = penColor_mc.getRGB(); aDraw.attributes.fc = fullColor_mc.getRGB(); dataOfDraw.appendChild(aDraw); //数据指针加一 dataPointer++; //设定Undo按钮为有效 undo_btn.enabled = true; } //按下Undo时 undo_btn.onRelease = function() { //设定Redo为有效 redo_btn.enabled = true; dataPointer--; showDraw(); if (dataPointer == -1) { this.enabled = false; } }; //按下Redo时 redo_btn.onRelease = function() { //设定Undo为有效 undo_btn.enabled = true; dataPointer++; showDraw(); if (dataPointer == dataOfDraw.childNodes.length-1) { this.enabled = false; } }; //显示图形 function showDraw() { //清除原有的图 canvas1.clear(); for (i=0; i<=dataPointer; i++) { var tempNode = dataOfDraw.childNodes[i]; style = tempNode.nodeName; //取得节点中的属性数据 startX = Number(tempNode.attributes.sx); startY = Number(tempNode.attributes.sy); endX = Number(tempNode.attributes.ex); endY = Number(tempNode.attributes.ey); ps = Number(tempNode.attributes.ps); pc = Number(tempNode.attributes.pc); fc = Number(tempNode.attributes.fc); //绘在画布上 drawOnCanvas(canvas1, style, ps, pc, fc); } } //清除板面 function onClearClick() { undo_btn.enabled = false; redo_btn.enabled = false; canvas1.clear(); dataPointer = -1; dataOfDraw = new XML(); } //存储 function onSaveClick() { var tempSO = SharedObject.getLocal("myDraw"); //建立临时的XML对象 var tempXML = new XML(dataOfDraw.toString()); //清除可以Redo的数据 for (i=tempXML.childNodes.length-1; i>dataPointer; i--) { tempXML.childNodes[i].removeNode(); } //放入SharedObject tempSO.data.draws = tempXML.toString(); tempSO.flush(); } //载入 function onLoadClick() { undo_btn.enabled = true; redo_btn.enabled = false; //取得SharedObject var tempSO = SharedObject.getLocal("myDraw"); dataOfDraw = new XML(tempSO.data.draws); dataPointer = dataOfDraw.childNodes.length -1; canvas1.clear(); //画出图 showDraw(); } 第二,第三帧AS见原文件 [/code] [url=http://bbs.flash8.net/bbs/UploadFile/2005-7/200579194846834.swf]查看[/url] [url=http://bbs.flash8.net/bbs/viewFile.asp?Boardid=10&ID=253376]下载[/url]