console
var sizeE;
var fontSize;
var allColor;
var mousePress = false;
var last = null;
var capi = false;
var inText = false;
var ziX,ziY;
var canvas = document.querySelector("#myCanvas");
var cxt = canvas.getContext("2d");
var canvas3 = document.getElementById('myCanvas3');
var imgsrc = document.getElementById("imgsrc");
function imgTest(){
var ele = this;
document.getElementById("canvasdiv").style.display = "block";
console.log(ele.clientWidth,ele.clientHeight);
canvas3.style.display = "block";
canvas3.width = ele.clientWidth;
canvas3.height = ele.clientHeight;
canvas.width= ele.clientWidth;
canvas.height= ele.clientHeight;
cxt.drawImage(ele, 0, 0, ele.clientWidth, ele.clientHeight);
}
imgsrc.onclick = imgTest;
imgsrc.addEventListener("touchstart",imgTest,false);
var eraseImg = document.getElementById("eraseImg");
var size = document.getElementsByName("eraseSize");
function caPi(){
for (var i = 0; i < size.length; i++) {
if (size[i].checked == true) {
sizeE = size[i].value;
eraseImg.style.width = size[i].value +"px";
eraseImg.style.height = size[i].value +"px";
}
}
}
var fontS = document.getElementById("fontSize");
function fnFontsize(){
fontSize = document.getElementById("fontSize").value;
}
fontS.onchange = fnFontsize;
fontS.addEventListener("touchstart",fnFontsize,false);
var color = document.getElementById("color");
function fnColor(){
allColor = document.getElementById("color").value;
}
color.onchange = fnColor;
color.addEventListener("touchstart",fnColor,false);
var erase = document.getElementById("erase");
var eraseSeries = document.getElementById("eraseSeries");
function penRubber(e){
if(erase.innerText == "擦皮"){
eraseSeries.style.display = "inline-block";
erase.innerText = "画笔";
capi = true;
}else{
eraseSeries.style.display = "none";
erase.innerText = "擦皮";
capi = false;
}
e.preventDefault();
}
erase.onclick = penRubber;
erase.addEventListener("touchstart",penRubber,false);
fnFontsize();
fnColor();
caPi();
var board = canvas3.getContext('2d');
var textInput = document.getElementById("textareaInput");
function beginDraw() {
mousePress = true;
caPi();
if(inText){
mousePress = false;
var event = window.event||event;
if(isTouch(event)) {
x = event.touches[0].pageX;
y = event.touches[0].pageY;
} else {
x = event.offsetX + event.target.offsetLeft;
y = event.offsetY + event.target.offsetTop;
}
textInput.style.display = "inline-block";
textInput.style.left = x+"px";
textInput.style.top = y+"px";
ziX = x;
ziY = y;
console.log(ziX,ziY);
inText = false;
var type = event.type;
if(type.indexOf('touch') >= 0) {
event.preventDefault();
}
}else{
writeZi();
}
}
function writeZi(){
console.log("画出文字了",ziX,ziY);
if(ziX != undefined && ziY != undefined){
board.font = fontSize+"px 宋体";
board.fillStyle = allColor;
board.fillText(textInput.value,ziX+2,ziY-36);
textInput.style.display = "none";
textInput.style.top = "50px";
textInput.style.left = "0px";
textInput.value = "";
}
}
textInput.onblur = writeZi;
function drawing() {
board.lineWidth = fontSize;
if(!capi){
console.log(fontSize,allColor);
board.strokeStyle = allColor;
var event = window.event||event;
event.preventDefault();
if(!mousePress) return;
var xy = pos(event);
if(last != null) {
board.beginPath();
board.lineCap="round";
board.moveTo(last.x, last.y);
board.lineTo(xy.x, xy.y);
board.stroke();
}
last = xy;
}else{
var event = window.event||event;
event.preventDefault();
if(!mousePress) return;
var xy = pos(event);
if(last != null) {
board.clearRect(xy.x, xy.y, sizeE, sizeE);
}
last = xy;
}
}
function endDraw() {
var event = window.event||event;
mousePress = false;
event.preventDefault();
last = null;
}
function pos(event) {
var x, y;
if(isTouch(event)) {
x = event.touches[0].pageX;
y = event.touches[0].pageY-50;
} else {
x = event.offsetX + event.target.offsetLeft;
y = event.offsetY + event.target.offsetTop-50;
}
return {
x: x,
y: y
};
}
function isTouch(event) {
var type = event.type;
if(type.indexOf('touch') >= 0) {
return true;
} else {
return false;
}
}
canvas3.onmousedown = beginDraw;
canvas3.onmousemove = drawing;
canvas3.onmouseup = endDraw;
canvas3.addEventListener('touchstart', beginDraw, false);
canvas3.addEventListener('touchmove', drawing, false);
canvas3.addEventListener('touchend', endDraw, false);
var inputText = document.getElementById("etext");
function displayInput(){
inText = true;
}
inputText.onclick = displayInput;
inputText.addEventListener('touchstart', displayInput, false);
function saveImg(){
cxt.drawImage(canvas3, 0, 0, 639, 640);
var dataUrl = canvas.toDataURL();
document.getElementById("tupian").src = dataUrl;
}
function cancelImg(){
board.clearRect(0, 0, canvas3.width, canvas3.height);
document.getElementById("canvasdiv").style.display = "none";
canvas3.style.display = "none";
}
<img src="https://t7.baidu.com/it/u=4162611394,4275913936&fm=193&f=GIF" class="img" id="imgsrc" />
<div class="canvasdiv" id="canvasdiv">
<div class="navbar" id="btnTeam">
<select id="color">
<option class="opt" value="red">红色</option>
<option class="opt" value="yellow">黄色</option>
<option class="opt" value="blue">蓝色</option>
<option class="opt" value="black" selected>黑色</option>
<option class="opt" value="green">绿色</option>
</select>
<select id="fontSize">
<option value="5" selected>5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="30">30</option>
</select>
<button id="erase" class="erase">擦皮</button>
<div class="eraseSeries" id="eraseSeries">
<input type="radio" name="eraseSize" value="5" checked="checked" />5
<input type="radio" name="eraseSize" value="10" />10
<input type="radio" name="eraseSize" value="15" /> 15
<input type="radio" name="eraseSize" value="20" /> 20
<input type="radio" name="eraseSize" value="30" />30
</div>
<button id="etext" class="etext">文字</button>
<button id="saveimg" class="saveimg" onclick="saveImg()">保存</button>
<button id="cancel" class="cancelimg" onclick="cancelImg()">取消</button>
</div>
<canvas id="myCanvas" class="bottomCanvas"></canvas>
<canvas id="myCanvas3" class="topCanvas"></canvas>
<div id="eraseImg">
</div>
<textarea id="textareaInput" class="drag"></textarea>
</div>
<img src="" id="tupian" />
*{margin: 0;padding: 0;}
HTML,body{width: 100%;height: 100%;overflow: hidden;}
.canvasdiv{width: 100%;height: 100%;position: absolute;display: none;
z-index: 5;top: 0;left: 0;background: #DADADA;overflow: hidden;-webkit-touch-callout:
none;-webkit-user-select: none;-khtml-user-select: none;-moz-user-select: none;
-ms-user-select: none;user-select: none;}
.navbar{width: 500px;height: 40px;margin: 5px 0;position: relative;}
.navbar a{display: inline-block;height: 40px;width: 40px;border-radius: 20px;
position: absolute;right: -20px;background: #B4A484;text-align: center;line-height:
40px;font-size: 30px;top: 0;-webkit-touch-callout: none;-webkit-user-select: none;
-khtml-user-select: none;-moz-user-select: none;-ms-user-select: none;user-select:
none;}
.eraseSeries{display: inline-block;height: 20px;margin: 10px 0 10px 15px;vertical-align:
middle;display: none;}
.eraseSeries input{width: 20px;height: 20px;vertical-align: middle;}
select,.erase,.etext,.saveimg,.cancelimg{height: 30px;margin: 5px 0px 5px 15px;vertical-align: middle;}
.erase,.etext,.saveimg,.cancelimg{padding: 0 10px;}
textarea{width: 200px;height: 30px;background: transparent;position: absolute;display:none;top: 50px;left: 0;border: 2px solid #c1c1c1;resize:both;}
#eraseImg {border: solid;color: gray;border-radius: 118px;width: 5px;height: 5px;position: absolute;display: none;}
.bottomCanvas{position: relative;left: 0;top: 0;z-index: 1;}
.centerCanvas{background: transparent;position: absolute;left: 0;top: 50px;z-index: 2;display: none;}
.topCanvas{background: transparent;position: absolute;left: 0;top: 50px;z-index: 3;display: none;}
.drag{z-index: 4;}