<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>T1</title>
<script src="js/jquery-1.11.1.min.js"></script>
<script src="js/extended.js"></script>
<script src="js/base.js"></script>
<script src="js/util.js"></script>
</head>
<body style="border:10px solid red;">
<br>
<button onclick="stage.curScene.scaleX*=1.05;stage.curScene.scaleY*=1.05">放大</button>
<button onclick="stage.curScene.scaleX*=0.95;stage.curScene.scaleY*=0.95">缩小</button>
<br>
aaaaaaaaaaaaaaa
<canvas id="canvas" width="1000px" height="600px" style="border:4px solid black;"></canvas>
<script>
function Stage(canvas){
this.canvas = canvas;
this.ctx =canvas.getContext("2d");
//this.ctx.fontStyle = "微软雅黑"
var me = this;
this.curScene = null;
//屏蔽canvas的右键菜单
canvas.oncontextmenu = function(ev){
ev = ev || event;
ev.returnValue = false;
}
canvas.onmousedown = function(ev){
ev = ev || event;
var px = (ev.clientX -canvas.offsetLeft+document.body.scrollLeft - canvas.width / 2) / me.curScene.scaleX + canvas.width / 2 - me.curScene.translateX;
var py = (ev.clientY -canvas.offsetTop+document.body.scrollTop - canvas.height / 2) / me.curScene.scaleY + canvas.height / 2 - me.curScene.translateY;
if(me.curScene.onClick){
me.curScene.onClick({
event:ev,
pointX:px,
pointY:py
});
}
//var t = new TextNode(px+","+py);
//t.setLocation(px,py);
//me.curScene.add(t);
me.curScene.currentElement = null;
me.curScene.childs.forEach(function(ele){
if(ele.elementType == "node" || ele.elementType=="container") {
if (ele.inBound(px, py)) {
ele.selected = true;
me.curScene.currentElement = ele;
ele.pointX = px - ele.x;
ele.pointY = py - ele.y;
} else {
if(!ev.ctrlKey){
ele.selected = false;
}
}
}
});
if(me.curScene.currentElement){
me.canvas.style.cursor = "move";
var curEle = me.curScene.currentElement;
if(curEle.onClick){
curEle.onClick({
event:ev,
pointX:curEle.pointX,
pointY:curEle.pointY
})
}
}else{
me.canvas.style.cursor = "pointer";
}
canvas.onmousemove = function(ev){
ev = ev || event;
canvas.mousemoveEventObj = ev;
//var px2 = (ev.clientX -canvas.offsetLeft - canvas.width / 2) / me.curScene.scaleX + canvas.width / 2 - me.curScene.translateX;
//var py2 = (ev.clientY -canvas.offsetTop - canvas.height / 2) / me.curScene.scaleY + canvas.height / 2 - me.curScene.translateY;
var px2 = (ev.clientX -canvas.offsetLeft+document.body.scrollLeft - canvas.width / 2) / me.curScene.scaleX + canvas.width / 2 - me.curScene.translateX;
var py2 = (ev.clientY -canvas.offsetTop+document.body.scrollTop - canvas.height / 2) / me.curScene.scaleY + canvas.height / 2 - me.curScene.translateY;
var curEle = me.curScene.currentElement;
canvas.px2 =px2;
canvas.py2 = py2;
if(curEle){
var dx = px2-curEle.pointX;
var dy = py2-curEle.pointY;
if(curEle.childs){
curEle.childs.forEach(function(ele){
ele.x+=dx-curEle.x;
ele.y+=dy-curEle.y;
});
}
curEle.x = dx;
curEle.y = dy;
}else{
if(me.curScene.dragEnable){
me.curScene.translateX +=px2-px;
me.curScene.translateY +=py2-py;
}
}
}
document.onmouseup = function(){
canvas.onmousemove = null;
canvas.onmouseup = null;
me.canvas.style.cursor = "default";
}
};
canvas.onmousewheel = function(ev){
var ev = ev || window.event;
if(ev.deltaY>0){
me.curScene.scaleY *= me.curScene.wheelZoom;
me.curScene.scaleX *= me.curScene.wheelZoom;
}else{
me.curScene.scaleY /= me.curScene.wheelZoom;
me.curScene.scaleX /= me.curScene.wheelZoom;
}
//ev.preventDefault();
}
document.onmousemove = function(ev){
ev = ev || event;
me.curScene.focus = null;
var px = (ev.clientX -canvas.offsetLeft+document.body.scrollLeft - canvas.width / 2) / me.curScene.scaleX + canvas.width / 2 - me.curScene.translateX;
var py = (ev.clientY -canvas.offsetTop+document.body.scrollTop - canvas.height / 2) / me.curScene.scaleY + canvas.height / 2 - me.curScene.translateY;
me.curScene.childs.forEach(function(ele){
if(ele.elementType == "node" || ele.elementType == "link"){
if(ele.inBound(px,py)){
me.curScene.focus = ele;
}
}
});
}
void function(){
if(me.curScene){
// me.canvas.height = parseInt(me.canvas.style.height)/me.curScene.scaleX;
// me.canvas.width = parseInt(me.canvas.style.width)/me.curScene.scaleY;
me.curScene.paint(me.ctx);
}
setTimeout(arguments.callee,20);
}();
}
function Scene(stage){
stage.curScene = this;
this.stage = stage;
this.childs = [];
this.scaleX = 1;
this.scaleY = 1;
this.translateX = 0;
this.translateY = 0;
this.bgStyle = "gray";
this.alpha = 255;
this.wheelZoom = 0.95;
this.dragEnable = true;//是否可以拖拽
this.onClick = function(params){
console.log(params);
}
this.dbClick = function(params){
console.log(params);
}
this.sort = function(){
this.childs.sort(function(a,b){
function ab(ele){
switch(ele.elementType){
case "container":
return 0;
case "link":
return 1;
case "node":
case "text":
return 3;
}
}
return ab(a)-ab(b);
});
}
this.add = function(ele){
if(this.childs.indexOf(ele)==-1){
this.childs.push(ele);
}
ele.scene = this;
this.sort();
}
this.paint = function(ctx){
var canvas = this.stage.canvas;
var ctx = this.stage.ctx;
this.childs.forEach(function(ele){
if(ele.elementType == "container"){
ele.childs.forEach(function(node){
if(node.x<ele.x){
node.x = ele.x;
}else if(node.x+node.width>ele.x+ele.width){
node.x = ele.x+ele.width-node.width;
}
if(node.y<ele.y){
node.y = ele.y;
}
else if(node.y+node.height>ele.y+ele.height){
node.y = ele.y+ele.height-node.height;
}
});
}
});
ctx.save();
//清除背景
ctx.save();
ctx.fillStyle = this.bgStyle;
ctx.fillRect(0,0,canvas.width,canvas.height);
ctx.restore();
ctx.translate(this.translateX,this.translateY);
ctx.translate(canvas.width/2-this.translateX,canvas.height/2-this.translateY);
ctx.scale(this.scaleX,this.scaleY);
ctx.fillStyle = "black";
var wh = 10;
ctx.fillRect(-wh/2,-wh/2,wh,wh);
ctx.translate(-canvas.width/2+this.translateX
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![unitypackage](https://img-home.csdnimg.cn/images/20210720083646.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/SVG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/HTML.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/HTML.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/HTML.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/HTML.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/HTML.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/HTML.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/HTML.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/HTML.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/HTML.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 37 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/d5b8e16cddf148da83293d5bb68d9224_admin_maxin.jpg!1)
博士僧小星
- 粉丝: 1946
- 资源: 5903
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)