Skip to main content

creating a small drawing tool using as3 where you can draw with mouse and record and replay it my clicking on replay button


code 
==============================================
//import required packages
import com.greensock.*;
import com.greensock.easing.*;
//
var drawing:Boolean=false;
var eraseing:Boolean=false;
var drawingbroad:Sprite;
var linedraw:Sprite;
var eraserDraw:Sprite;
var xloc:Array=new Array();
var yloc:Array=new Array();
var type:Array=new Array();
var replayline:Sprite;
var replayeraser:Sprite;
var startpointPencil:Boolean=false;
var startpointeraser:Boolean=false;
var toolsArray=new Array("pencil","eraser","replay");
//
var recordconter:Number=-1;
var tooltype:String=null;
var recordTimer:Timer;
init();
function init() {
newDrawing();
addnewdrawing();
selectaTool();
}
function newDrawing():void {
clearAll()
drawingbroad=new Sprite();
this.addChild(drawingbroad);
drawingbroad.x=10;
drawingbroad.y=10;
drawingbroad.graphics.lineStyle(1,0x000000);
drawingbroad.graphics.beginFill(0xFFFFFF);
drawingbroad.graphics.drawRect(10,10,750,500);
drawingbroad.graphics.endFill();
drawingbroad.alpha=0;
TweenLite.to(drawingbroad, 1, {alpha:1});
dispatchpencilEvents();
}
function selectaTool():void {
pencil.buttonMode=true;
eraser.buttonMode=true;
replay.buttonMode=true;

}
//
function dispatchpencilEvents():void {
drawingbroad.addEventListener(MouseEvent.MOUSE_UP,selectedToofalse);
drawingbroad.addEventListener(MouseEvent.MOUSE_DOWN,selectedTootrue);
pencil.addEventListener(MouseEvent.MOUSE_DOWN,selectpencil);
drawingbroad.addEventListener(MouseEvent.MOUSE_MOVE,startDrawing);
eraser.addEventListener(MouseEvent.MOUSE_DOWN,selecteraser);
replay.addEventListener(MouseEvent.MOUSE_DOWN,showreplayaction);
}
function showreplayaction(e:MouseEvent):void {
tooltype="replay";
toolactive(tooltype);
recordTimer=new Timer(10,xloc.length);
recordTimer.addEventListener("timer",showrecordedaction);
recordTimer.start();
}
function clearAll():void
{
recordconter=0;
startpointPencil=false;
xloc=[];
yloc=[];
type=[];
}
function showrecordedaction(t:TimerEvent):void {
recordconter+=1;
if((recordconter+2)==xloc.length)
{
clearAll();
recordTimer.removeEventListener("timer",showrecordedaction);
}
type[recordconter];
if (type[recordconter]=="pencil") {
if (!startpointPencil) {
replayline=new Sprite();
replayline.graphics.moveTo(xloc[recordconter],yloc[recordconter]);
startpointPencil=true;
drawingbroad.addChild(replayline);
} else {
replaypencilaction(recordconter);
}
}  if (type[recordconter]=="eraser") {

replayeraseraction();
}
}
function replayeraseraction() {
replayeraser=new Sprite();
replayeraser.graphics.beginFill(0xFFFFFF);
replayeraser.graphics.drawRect(xloc[recordconter],yloc[recordconter],50,50);
replayeraser.graphics.endFill();
drawingbroad.addChild(replayeraser);


}
function replaypencilaction(recordconter):void {

replayline.graphics.lineStyle(1,0x000000);
replayline.graphics.lineTo(xloc[recordconter],yloc[recordconter]);
replayline.graphics.endFill();
drawingbroad.addChild(replayline);


}

function selectedToofalse(m:MouseEvent):void {
if (tooltype=="pencil") {
drawing=false;
eraseing=false;
} else if (tooltype=="eraser") {
drawing=false;
eraseing=false;
}
//TweenLite.to(linedraw, 0.5, {alpha:1});
}
function selectedTootrue(m:MouseEvent):void {
if (tooltype=="pencil") {
drawing=true;
eraseing=false;
} else if (tooltype=="eraser") {
drawing=false;
eraseing=true;
}
linedraw=new Sprite();
linedraw.graphics.moveTo(linedraw.mouseX,linedraw.mouseY);

}


//select a tool
function startDrawing(m:MouseEvent):void {
if (drawing && tooltype=="pencil") {
linedraw.graphics.lineStyle(1,0x000000);
linedraw.graphics.lineTo(drawingbroad.mouseX,drawingbroad.mouseY);
linedraw.graphics.endFill();
linedraw.alpha=0.2;
drawingbroad.addChild(linedraw);
xloc.push(mouseX);
yloc.push(mouseY);
type.push("pencil");

} else if (eraseing && tooltype=="eraser") {

eraserDraw=new Sprite();
eraserDraw.graphics.beginFill(0xFFFFFF);
if (drawingbroad.mouseX>(drawingbroad.width-50)) {
eraserDraw.graphics.drawRect(drawingbroad.mouseX-50,drawingbroad.mouseY,50,50);
} else if (drawingbroad.mouseY>(drawingbroad.height-50)) {
eraserDraw.graphics.drawRect(drawingbroad.mouseX,drawingbroad.mouseY-50,50,50);
} else {
eraserDraw.graphics.drawRect(drawingbroad.mouseX,drawingbroad.mouseY,50,50);
}

eraserDraw.graphics.endFill();
drawingbroad.addChild(eraserDraw);
xloc.push(mouseX);
yloc.push(mouseY);
type.push("eraser");
}
}

function selectpencil(m:MouseEvent):void {

tooltype="pencil";
toolactive(tooltype);


}

function selecteraser(m:MouseEvent):void {
tooltype="eraser";
toolactive(tooltype);
}

function toolactive(__tooltype):void {

switch (__tooltype) {
case "pencil" :
workwithpencil();
break;
case "eraser" :
workwitheraser();
break;
case "replay" :
workwithreplay();
break;
}
}
function activeMe():void {
for (var i in toolsArray) {
if (tooltype==toolsArray[i]) {
TweenLite.to(this[toolsArray[i]], 1, {alpha:0.2});
} else {
TweenLite.to(this[toolsArray[i]], 1, {alpha:1});
}
}
}
function workwithpencil():void {

activeMe();
}
function workwitheraser():void {
activeMe();
}
function workwithreplay():void {
activeMe();
}
//
function addnewdrawing():void {
newd.addEventListener(MouseEvent.CLICK,refreshpage);
}
function refreshpage(m:MouseEvent):void {
TweenLite.to(drawingbroad, 1, {alpha:0,onComplete:removeDrawing});

}
function removeDrawing():void {
if (drawingbroad) {
removeChild(drawingbroad);
drawingbroad=null;
}
newDrawing();
}

screen shot
==========================================





Comments

Popular posts from this blog

Creating a word Scramble game where you can drag and drop the word and create a complete word. (drag and drop word Scramble game).

Creating a word game using  phaser game engine which is a javascript based gaming engine  where u can drag and drop world like Word Scramble Game and complete a given word. your index.html file look like below < html > < head > < script src = "src/phaser.min.js" ></ script > < script src = "src/wordGame.js" ></ script > < script src = "src/main.js" ></ script > </ head > < body > </ body > </ html > 1.    <script src="src/phaser.min.js"></script> required to run the  phaser game engine. 2. rest of the two files deals with game logics      <script src="src/wordGame.js"></script>       <script src="src/main.js"></script> you can download whole project from https://github.com/manishchauhan/wordgame/tree/master just copy and paste the project in your local server and run ...

starting with three.js and react with a very simple example

 1. three.js is undoubtedly the best library to create interactive content for the web in 3d. link https://threejs.org/ a working sample of three with react can be found below https://stackblitz.com/edit/react-7n5qf9?file=src%2FApp.js why choose threejs 1. lightweight  2.fast 3.big community  https://discourse.threejs.org/ 4. even you can use unity or unreal to publish html5 content but i don't think that would be acceptable in many cases. 5. open-source project. i created a running sample with React  https://stackblitz.com/edit/react-7n5qf9?file=src%2FApp.js

A simple binary search tree with generic approach tree can store any type of data. (DFS and BFS)

 1. a binary search tree in c++ using a generic approach with both BFS and DFS approach    working tree    https://www.onlinegdb.com/edit/HkuQLdA-w code=> some properties of binary search tree-> 1. binary search tree is not always a balanced tree.  2. Inorder, traversing gives you sorted data.  3.  best is o(log n) and worst is O(n).  4. left node data is always less than root data and right node data always be greater than root data.  5. red-black tree STL map is a balanced binary search tree. *******************************************************************************/ #include <stdio.h> #include<iostream> #include<queue> using namespace std; template < typename T > class Node {   //can store any type of data private:   //genric data or store any type of data   T data;   //reference of left pointer   Node *left = nullptr;   //reference of right pointer   Node *rig...