<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style>
body { margin-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px;}
.menu{
margin-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px;
!--border:none;
text-align: center;
width:100px;
height:60px;
background:#81E1F7;
cursor:pointer ;
}
.menu1{
margin-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px;
border:none;
text-align: center;
width:100px;
height:30px;
background:#ffffff;
cursor:pointer ;
}
.menu2{
margin-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px;
border:none;
text-align: right;
width:100px;
height:60px;
background:#81E1F7;
cursor:pointer ;
}
#menuBody{
border-collapse: collapse;
border:2px solid #00FFFF;
background:#00FFFF;
}

</style>
</head>
<body>
<div align="center" >
<div );
soBoringing.onmouseover = function() {
soBoringing.style.cursor = 'crosshair';
}



//点击清除时执行
function chearClick(){
context.clearRect(0,0,width,height);
data=context.getImageData(0, 0, width, height);
console.log(wholeImage);
}


//打开保存图像
function openClick(){
for(var i=0;i<objectData.length;i++){
objectData[i].drew();
}
for(i=0;i<finalCurveArray.length;i++)
for(j=0;j<finalCurveArray[i].length;j++)
finalCurveArray[i][j].drew();

}


//保存图像
function saveClick(){
for(var i=0;i<wholeImage.length;i++){
objectData[i] = wholeImage[i];
}
for(i=0;i<curveArray.length;i++)
finalCurveArray[i]= new Array();
for(j=0;j<curveArray[i].length;j++)
finalCurveArray[i][j] = curveArray[i][j];

}



//所有类型构造函数
function shapeFather(theShape,theSize,theColor,theOrignalX,theOrignalY,theLastX, theLastY,newContext){
this.theShape=theShape;
this.theOrignalX=theOrignalX;
this.theOrignalY=theOrignalY;
this.theLastX=theLastX;
this.theLastY=theLastY;
this.color = theColor;
this.size = theSize;

this.context2 = newContext;
this.context2.strokeStyle = this.color;
this.context2.lineWidth = this.size;

}



shapeFather.prototype.drew=function(){
var context1 = this.context2;
context1.strokeStyle = this.color;
context1.lineWidth = this.size;
var orignalX = this.theOrignalX;
var orignalY = this.theOrignalY;
var lastX = this.theLastX;
var lastY = this.theLastY;
var Shape = this.theShape;
switch(Shape){
case 0:
context1.beginPath();
context1.arc(orignalX+(lastX-orignalX)/2,orignalY+(lastY-orignalY)/2,Math.abs(lastX-orignalX)/2,0,Math.PI * 2,true);
context1.stroke();
context1.closePath();
break;
case 1:
context1.beginPath();
context1.strokeRect(orignalX, orignalY, lastX-orignalX, lastY-orignalY);
context1.closePath();
break;
case 2:
//
context1.beginPath();
context1.moveTo(this.orignalX,this.orignalY);
context1.lineTo(this.lastX,this.lastY);
context1.stroke();
context1.closePath();
   //alert("1111");
break;
}

}

document.getElementById('color').onchange = function(){
     context.strokeStyle = this.value
};




//点击橡皮时执行
function doEraser(){
myCanvas.removeEventListener("mousemove", myCanvasMouseMove, false);
myCanvas.removeEventListener("mousedown", myCanvasMouseDown,false);
myCanvas.removeEventListener("mouseup", myCanvasMouseUp, false);
myCanvas.addEventListener("click", myCanvasMouseClick);

}



// 改变画笔粗细
function sizeChange(){

context.lineWidth = parseInt(document.getElementById('size').value);

}





// 改变所画类型
function shapeChange(){
myCanvas.addEventListener("mousedown", myCanvasMouseDown, false);
myCanvas.addEventListener("mousemove", myCanvasMouseMove, false);
myCanvas.addEventListener("mouseup", myCanvasMouseUp, false);
myCanvas.removeEventListener("click", myCanvasMouseClick);
context.strokeStyle = "black";
var myselect = document.getElementById("shape");
var index=myselect.selectedIndex ;  
var myvalue  = myselect.options[index].value;
var mytext=myselect.options[index].text; 
shap = parseInt(myvalue);
}




function myCanvasMouseDown(event) {
//event.preventDefault();

if(event.button == 0) {

orignalX = event.offsetX;
orignalY = event.offsetY;
data = context.getImageData(0, 0, width, height);
isMouseDown = true;

}
if(shap==2)
{
curveArray[curveObjectCount]= new Array();

}
}


function myCanvasMouseMove(event) {
if (isMouseDown){

switch(shap){
case 0:
context.clearRect(0,0,width,height);
context.putImageData(data,0,0);
lastX = event.offsetX;
lastY = event.offsetY;
context.beginPath();
context.arc(orignalX+(lastX-orignalX)/2,orignalY+(lastY-orignalY)/2,Math.abs(lastX-orignalX)/2,0,Math.PI * 2,true);
context.stroke();
context.closePath();
break;
case 1:
context.clearRect(0,0,width,height);
context.putImageData(data,0,0);
lastX = event.offsetX;
lastY = event.offsetY;
context.strokeRect(orignalX, orignalY, lastX-orignalX, lastY-orignalY);
break;
case 2:
context.clearRect(0,0,width,height);
context.putImageData(data,0,0);
lastX = event.offsetX;
lastY = event.offsetY;
context.moveTo(orignalX,orignalY);
context.lineTo(lastX,lastY);
var graph =new shapeFather(shap,context.lineWidth,context.strokeStyle,orignalX ,orignalY,lastX,lastY,context);
curveArray[curveObjectCount][curveObjectCounter]=graph;
orignalX=lastX;
orignalY=lastY;
context.stroke()
curveObjectCounter++;
break;
case 3:
break;

}
}
}



function myCanvasMouseUp(event) {

if (isMouseDown){

context.clearRect(0,0,width,height);
context.putImageData(data,0,0);
lastX = event.offsetX;
lastY = event.offsetY;
isMouseDown = false;
lineW=context.lineWidth;
strokeS=context.strokeStyle;
if(shap==2)
{ //alert(curveArray.length);

for(i=0;i<curveArray.length;i++)
for(j=0;j<curveArray[i].length;j++);
//curveArray[i][j].theShape=2;
//alert(curveArray[0][0].theShape);
//curveArray[i][j].drew();
curveObjectCount++;
curveObjectCounter=0;

   context.lineTo(lastX, lastY); //根据鼠标路径绘画  
    context.stroke();   //立即渲染 




}
else
{
var pic = new shapeFather(shap,lineW,strokeS,orignalX ,orignalY,lastX,lastY,context);
pic.drew();
wholeImage.push(pic);
}

isMouseDown = false;
lastX = null;
lastY = null;

}
}

//鼠标点击事件
function myCanvasMouseClick(event){
context.clearRect(0,0,1920,810);
context.beginPath();
for(i=0;i<wholeImage.length;i++)
{

if(wholeImage[i].theShape==0)
{
wholeImage[i].drew();
if(context.isPointInStroke(event.offsetX,event.offsetY))
{
wholeImage.splice(i,1);
break;
}
}
if(wholeImage[i].theShape==1)
{
wholeImage[i].drew();
if(context.isPointInStroke(event.offsetX,event.offsetY))
{
wholeImage.splice(i,1);
break;
}
}
}
context.closePath();
context.clearRect(0,0,1920,810);
for(i=0;i<wholeImage.length;i++)
wholeImage[i].drew();
}

</script>

</html>

相关文章:

  • 2021-10-14
  • 2022-02-28
  • 2021-05-27
  • 2022-01-26
  • 2022-12-23
  • 2022-12-23
  • 2021-09-07
  • 2021-06-26
猜你喜欢
  • 2022-01-18
  • 2022-02-26
  • 2022-02-04
  • 2021-09-14
  • 2021-09-18
  • 2021-12-23
相关资源
相似解决方案