exports.install = function (Vue, options) {
Vue.prototype.shieldFn = function (canvas,ctx) {
var _sThis = this;
var shieldPosi = [],sendShieldPosi=[],allShieldPosi = [];
var shield_count = 0;
var dbclick = false;
var imgData;
var image = new Image();
image.src = _sThis.$store.state.exportImgUrl;
image.onload = function(){
ctx.drawImage(image,0,0,canvas.width,canvas.height);
}
canvas.onmousedown = function (event) {
var downPosi = _sThis.windowToCanvas(canvas,event.clientX,event.clientY);
var isIn = _sThis.pointInPoly(downPosi,_sThis.$store.state.sendMonitorPosi);
if(isIn){
shieldPosi.push(downPosi);
if(shieldPosi.length>3){
var polygoning = _sThis.convex(shieldPosi,shieldPosi.length);
if(polygoning!=1){
shieldPosi.pop(downPosi);
alert("您绘制的不是凸多边形,请重新绘制");
return;
}
}
}else{
alert("只能在监控区域内绘制")
}
canvas.onmousemove = function (ev) {
var movePosi = _sThis.windowToCanvas(canvas,ev.clientX,ev.clientY);
drawLine(ctx,movePosi.x,movePosi.y,shieldPosi,2,"black","skyblue","rgba(255,255,255,0.8)");
}
}
canvas.ondblclick = function (e) {
var dbPosi = _sThis.windowToCanvas(canvas,e.clientX,e.clientY);
var polygon = _sThis.convex(shieldPosi,shieldPosi.length);
if(polygon!=1){
alert("您绘制的不是凸多边形,请重新绘制");
return;
}
shieldPosi.push(dbPosi);
var shieldEndPosis = _sThis.endPosi(shieldPosi,sendShieldPosi);
drawLine(ctx,dbPosi.x,dbPosi.y,shieldPosi,2,"black","skyblue","rgba(255,255,255,0.8)");
shield_count++;
allShieldPosi.push(sendShieldPosi);
_sThis.$store.commit("getShieldPosi",allShieldPosi)
console.log("所有屏蔽区最终坐标========",_sThis.$store.state.shieldEndPosi);
canvas.onmousemove = null;
imgData = ctx.getImageData(0,0,canvas.width,canvas.height);
sendShieldPosi = [];
shieldPosi = [];
dbclick = true;
}
function drawLine(ctx,moveX,moveY,posi,lineWidth,lineColor,arcFillColor,areaColor){
ctx.clearRect(0,0,canvas.width,canvas.height);
if(dbclick){
ctx.putImageData(imgData,0,0);
}else{
ctx.drawImage(image,0,0,canvas.width,canvas.height);
}
ctx.beginPath();
ctx.lineJoin = 'round';
ctx.strokeStyle = lineColor;
ctx.fillStyle = areaColor;
ctx.lineWidth = lineWidth;
if(posi.length!=0){
ctx.moveTo(posi[0].x,posi[0].y);
}
for(var i = 0; i < posi.length ; i++){
ctx.lineTo(posi[i].x,posi[i].y);
}
ctx.lineTo(moveX,moveY);
ctx.fill();
ctx.closePath();
ctx.stroke();
ctx.beginPath();
ctx.fillStyle = arcFillColor;
for(var i =0; i < posi.length;i++){
ctx.arc(posi[i].x, posi[i].y,5,0,2*Math.PI);
ctx.closePath();
ctx.fill();
}
}
}
Vue.prototype.clearAllShield = function (canvas,ctx) {
ctx.clearRect(canvas,0,0,canvas.width,canvas.height);
this.imgToCanvas(canvas,ctx,this.$store.state.exportImgUrl);
}
}