matter.js学习笔记(三)--mouseConstraint鼠标控制
来源:互联网 发布:图像软件有哪些 编辑:程序博客网 时间:2024/05/22 10:25
写在前面
mouseConstraint默认为世界里的物体添加鼠标约束。
1. 先上源码
MouseConstraint.create = function(engine, options) { var mouse = (engine ? engine.mouse : null) || (options ? options.mouse : null); if (!mouse) { if (engine && engine.render && engine.render.canvas) { mouse = Mouse.create(engine.render.canvas); } else if (options && options.element) { mouse = Mouse.create(options.element); } else { mouse = Mouse.create(); Common.warn('MouseConstraint.create: options.mouse was undefined, options.element was undefined, may not function as expected'); } } var constraint = Constraint.create({ label: 'Mouse Constraint', pointA: mouse.position, pointB: { x: 0, y: 0 }, length: 0.01, stiffness: 0.1, angularStiffness: 1, render: { strokeStyle: '#90EE90', lineWidth: 3 } }); var defaults = { type: 'mouseConstraint', mouse: mouse, element: null, body: null, constraint: constraint, collisionFilter: { category: 0x0001, mask: 0xFFFFFFFF, group: 0 } }; var mouseConstraint = Common.extend(defaults, options); Events.on(engine, 'beforeUpdate', function() { var allBodies = Composite.allBodies(engine.world); MouseConstraint.update(mouseConstraint, allBodies); _triggerEvents(mouseConstraint); }); return mouseConstraint; };
if (!mouse) {
if (engine && engine.render && engine.render.canvas) {
mouse = Mouse.create(engine.render.canvas);
} else if (options && options.element) {
mouse = Mouse.create(options.element);
} else {
mouse = Mouse.create();
Common.warn('MouseConstraint.create: options.mouse was undefined, options.element was undefined, may not function as expected');
传入的参数中如果没有mouse,但是存在engine,并且engine.render和engine.render.canvas都存在,则会默认创建鼠标。最后,返回mouseConstraint 。
2.用法
只需引入Matter.MouseConstraint,并且生成鼠标控制实例 var mouseConstraint=MouseConstraint.create(engine,{});
,再将它添加到世界中,
World.add(world,[rectA,ground,stack_rect,stack_circle,mouseConstraint]);
3.ps
为render的options添加wireframes:false
可以为世界里的物品添加颜色,如果不对物品设置渲染颜色的话,引擎会默认物体颜色随机。如果要指定物体的颜色,可以在生成物体时设置render:render:{fillStyle:"#9fa"}
。
4.设置世界全屏
在生成render时,可以设置options选项,其中,有width和height属性,分别设置为$(window)的width()和height()即可。同时,要对body设置style:
body{ margin:0; overflow: hidden;}
5.代码全文
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>test</title> <script src="../build/matter.js"></script> <script src="../demo/lib/jquery-1.11.0.min.js"></script> <style> body{ margin:0; overflow: hidden; } </style></head><body><script> var Engine=Matter.Engine, Render=Matter.Render, World=Matter.World, Mouse=Matter.Mouse, MouseConstraint=Matter.MouseConstraint, Composites=Matter.Composites, Bodies=Matter.Bodies; var engine=Engine.create(), world=engine.world; var render=Render.create({ engine:engine, element:document.body, options:{ width:$(window).width(), height:$(window).width(), wireframes:false } }); Engine.run(engine); Render.run(render); var rectA=Bodies.rectangle(100,20,200,40,{ render:{ fillStyle:"#f0c" } }); var ground=Bodies.rectangle($(window).width()/2,$(window).height()-10,$(window).width(),20,{ isStatic:true, render:{ fillStyle:"#9fa" } }); var stack_rect=Composites.stack(300,100,4,3,0,0,function(x,y){ return Bodies.rectangle(x,y,150,40); }); var stack_circle=Composites.stack(1200,100,5,5,2,3,function (x,y) { return Bodies.circle(x,y,30); }); var mouseConstraint=MouseConstraint.create(engine,{}); World.add(world,[rectA,ground,stack_rect,stack_circle,mouseConstraint]);</script></body></html>
效果
- matter.js学习笔记(三)--mouseConstraint鼠标控制
- matter.js学习笔记(一)
- matter.js学习笔记(二)--Composites.stack()分析
- matter.js学习笔记(四)--Constraint.create()制造跷跷板
- matter.js学习笔记(五)--Composites.chain()制造铁索桥
- matter.js学习笔记(七)--Composites.car()制造汽车
- matter.js学习笔记(六)--Composites.softBody()制造布料或球网
- matter.js学习笔记(八)--Composites.newtonsCradle()制造牛顿摆
- matter.js学习笔记(九)--Body的parts属性创造十字形物品
- JS学习笔记(三)
- JS学习笔记(三)
- Java学习笔记(三)-- 流程控制
- JS学习笔记(三)
- Python Selenium 学习笔记(三)键盘和鼠标操作
- Python Selenium 学习笔记(三)键盘和鼠标操作
- JS控制鼠标滚轮
- augular.js 菜鸟学习笔记 (三)
- JS面向对象学习笔记(三)
- ASP.NET MVC 随想录—— 使用ASP.NET Identity实现基于声明的授权,高级篇
- 【IT软技能】Camtasia Studio 9注册激活
- 第十二章——子类化UITableViewCell
- 字符编码
- 在ElementaryOS下安装中文输入法
- matter.js学习笔记(三)--mouseConstraint鼠标控制
- 【HDU6196 2017 ACM ICPC Asia Regional Shenyang Online C】【爆搜 + 剪枝】happy happy happy 爸爸儿子轮流两头取数 爸爸想输且输少
- 04-VTK可视化管线(1)
- 树梅派-控制二极管灯闪烁脚本(python)
- [LeetCode]3. Longest Substring Without Repeating Characters
- poj 3311 Hie with the Pie(TSP ,状压dp)
- nodejs初步学习遇到错误
- Java中@Override的作用
- 堆 相关笔试题