KineticJS Canvas - Scale group from center point(KineticJS Canvas - 从中心点缩放组)
问题描述
我想扩展我的组(图像和其他内容).
group.setScale(zoom, zoom);
<!DOCTYPE HTML><html><头></头><身体><button id="larger">更大的</button><div id="container"></div><script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.4.0.min.js"></script><script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script><脚本>$(函数(){var stage = new Kinetic.Stage({容器:'容器',宽度:400,身高:400});var layer = new Kinetic.Layer();//一定要设置宽高//他们是这个方法工作所必需的var group = new Kinetic.Group({×:100,y: 100,宽度:100,高度:100});//存储原始组中心//这样我们就可以将组居中group.cx=group.getX()+group.getWidth()/2;group.cy=group.getY()+group.getHeight()/2;//自定义缩放功能//缩放组并使结果居中group.scale=function(x,y){group.setScale(x,y);group.setPosition(group.cx-group.getWidth()/2*group.getScale().x,group.cy-group.getHeight()/2*group.getScale().y);group.draw();}var box1 = new Kinetic.Rect({x: 0,y: 0,宽度:50,身高:50,填充:蓝色",中风:'黑色',行程宽度:4});group.add(box1);var box2 = new Kinetic.Rect({×:50,y: 50,宽度:50,身高:50,填写:绿色",中风:'黑色',行程宽度:4});group.add(box2);层.添加(组);阶段.添加(层);变比例因子=1;$("#larger").click(function(){比例因子+=0.10;group.scale(scaleFactor,scaleFactor);控制台.log(比例因子);});});</脚本></身体></html>
I want to scale my group (image and something).
group.setScale(zoom, zoom);
http://jsfiddle.net/K8nK3/
But when i scale my group, I think it's not scale from center of my group. i think it like this
I want it scale from center like
I try more but it's not success. How can i do that, thanks.
[Edited to better fit questioner's needs]
Here’s how to scale a Kinetic group from the centerpoint
First we store the centerX and centerY of the original group so we can keep the group centered there:
group.cx=group.getX()+group.getWidth()/2;
group.cy=group.getY()+group.getHeight()/2;
Then we write a custom scaling method that both scales the group and re-centers it:
group.scale=function(x,y){
group.setScale(x,y);
group.setPosition(
group.cx-group.getWidth()/2*group.getScale().x,
group.cy-group.getHeight()/2*group.getScale().y);
group.draw();
}
Here’s code and a Fiddle: http://jsfiddle.net/m1erickson/dVGGz/
<!DOCTYPE HTML>
<html>
<head></head>
<body>
<button id="larger">Larger</button>
<div id="container"></div>
<script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.4.0.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script>
$(function(){
var stage = new Kinetic.Stage({
container: 'container',
width: 400,
height: 400
});
var layer = new Kinetic.Layer();
// Be sure to set width and height
// They are required for this method to work
var group = new Kinetic.Group({
x: 100,
y: 100,
width:100,
height:100
});
// store the original group center
// so we can center the group there
group.cx=group.getX()+group.getWidth()/2;
group.cy=group.getY()+group.getHeight()/2;
// custom scale function to both
// scale the group and center the results
group.scale=function(x,y){
group.setScale(x,y);
group.setPosition(
group.cx-group.getWidth()/2*group.getScale().x,
group.cy-group.getHeight()/2*group.getScale().y);
group.draw();
}
var box1 = new Kinetic.Rect({
x: 0,
y: 0,
width: 50,
height: 50,
fill: "blue",
stroke: 'black',
strokeWidth: 4
});
group.add(box1);
var box2 = new Kinetic.Rect({
x: 50,
y: 50,
width: 50,
height: 50,
fill: "green",
stroke: 'black',
strokeWidth: 4
});
group.add(box2);
layer.add(group);
stage.add(layer);
var scaleFactor=1;
$("#larger").click(function(){
scaleFactor+=0.10;
group.scale(scaleFactor,scaleFactor);
console.log(scaleFactor);
});
});
</script>
</body>
</html>
这篇关于KineticJS Canvas - 从中心点缩放组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:KineticJS Canvas - 从中心点缩放组
- Fetch API 如何获取响应体? 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 400或500级别的HTTP响应 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01