HTML5:Canvas复杂的拖拽和KineticJS界限教程
2013-10-21 21:20
316 查看
限制的移动节点被拖掉KineticJS内部地区,我们可以使用dragBoundFunc属性定义的边界节点不能交叉。
产品说明:拖放浅蓝色矩形和观察,它必然低于一个假想边界在y = 50。拖放黄色矩形和观察,它必然在一个假想的圆。
产品说明:拖放浅蓝色矩形和观察,它必然低于一个假想边界在y = 50。拖放黄色矩形和观察,它必然在一个假想的圆。
<!DOCTYPE HTML> <html> <head> <style> body { margin: 0px; padding: 0px; } </style> <body> <div id="container"></div> <script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.7.2.min.js"></script> <script defer="defer"> var stage = new Kinetic.Stage({ container: 'container', width: 578, height: 200 }); var layer = new Kinetic.Layer(); // bound below y=50 var blueGroup = new Kinetic.Group({ x: 100, y: 70, draggable: true, dragBoundFunc: function(pos) { var newY = pos.y < 50 ? 50 : pos.y; return { x: pos.x, y: newY }; } }); // bound inside a circle var yellowGroup = new Kinetic.Group({ x: stage.getWidth() / 2, y: 70, draggable: true, dragBoundFunc: function(pos) { var x = stage.getWidth() / 2; var y = 70; var radius = 50; var scale = radius / Math.sqrt(Math.pow(pos.x - x, 2) + Math.pow(pos.y - y, 2)); if(scale < 1) return { y: Math.round((pos.y - y) * scale + y), x: Math.round((pos.x - x) * scale + x) }; else return pos; } }); var blueText = new Kinetic.Text({ fontSize: 26, fontFamily: 'Calibri', text: 'bound below', fill: 'black', padding: 10 }); var blueRect = new Kinetic.Rect({ width: blueText.getWidth(), height: blueText.getHeight(), fill: '#aaf', stroke: 'black', strokeWidth: 4 }); var yellowText = new Kinetic.Text({ fontSize: 26, fontFamily: 'Calibri', text: 'bound in circle', fill: 'black', padding: 10 }); var yellowRect = new Kinetic.Rect({ width: yellowText.getWidth(), height: yellowText.getHeight(), fill: 'yellow', stroke: 'black', strokeWidth: 4 }); blueGroup.add(blueRect).add(blueText); yellowGroup.add(yellowRect).add(yellowText); layer.add(blueGroup); layer.add(yellowGroup); stage.add(layer); </script> </head> </body> </html>
相关文章推荐
- HTML5新的东西:Canvas里面KineticJS补间动画教程!
- [js高手之路] html5 canvas系列教程 - arc绘制曲线图形(曲线,弧线,圆形)
- html5 canvas 详细使用教程
- HTML5 canvas 元素详细教程七
- HTML5教程实例-Canvas标签-坐标变换与路径结合使用
- HTML5那些故事:Canvas的KineticJS图片教程!
- HTML5中使用canvas绘制复杂图形
- 给力的 Google HTML5 训练营(HTML5 Drag&Drop 拖拽、FileReader实例教程
- HTML5 canvas 基础入门教程
- Html5添加SVGcanvas和gauge.js制作的十分精美的仪表盘插件教程
- html5 Canvas画图教程(7)―canvas里画曲线之quadraticCurveTo方法
- HTML5教程-用Canvas标签绘制矩形
- 非常赞的html5的在线canvas教程
- HTML5 canvas 初级入门教程
- 超酷的HTML5 Canvas网络画板教程
- HTML5里面的东西:Canvas KineticJS规模动画教程!
- [js高手之路]html5 canvas教程 - 1px问题以及绘制坐标系网格
- HTML5里面的知识:Canvas的KineticJS多边形教程
- [转]html5 Canvas画图教程(5)—canvas里画曲线之arc方法
- html5 Canvas画图教程(3)―canvas出现1像素线条模糊不清的原因