简体   繁体   English

KineticJS-获取路径形状的宽度/高度?

[英]KineticJS - Get the width/height of a Path Shape?

this is a big issue for me at the moment... 目前这对我来说是个大问题...

I'm rendering a world map via Path shapes... problem is the Path Shape does not offer width or height, so caching or other simple operations become really hard to do. 我正在通过路径形状渲染世界地图...问题是路径形状不提供宽度或高度,因此缓存或其他简单操作变得非常困难。

eg what x/y height/width to I give to path.toImage ? 例如我给path.toImage x / y高度/宽度是path.toImage


Any idea how I could get around this problem? 知道如何解决这个问题吗?

如果世界地图路径由一系列线组成(通常是这样),则可以通过在路径数据中查找minX / minY和maxX / maxY坐标来获得路径的边界框。

I just had to solve this same problem with Kinetic JS. 我只需要用Kinetic JS解决这个问题。

This code below looks through some random canvas draw code which contains (x,y) coordinates, extracts the coordinates into an array, looks through for the min and max of both X and Y, and shows the width and height of the shape. 下面的代码通过一些随机的画布绘制代码进行查看,其中包含(x,y)坐标,将坐标提取到一个数组中,查看X和Y的最小值和最大值,并显示该形状的宽度和高度。

From this you should have a bounding box. 由此您应该有一个边界框。

var regex = /[+-]?\d+\.\d+/g;

var str = 'ctx.bezierCurveTo(30.1, 220.7, 82.8, 233.0, 147.8, 233.0); ctx.bezierCurveTo(213.1, 233.0, 266.6, 220.8, 266.6, 205.7); ctx.bezierCurveTo(266.6, 205.3, 267.0, 205.1, 267.0, 204.7);';

var floats = str.match(regex).map(function(v) { return parseFloat(v); });

console.log(floats);

      var minX = 99999;
      var maxX = -99999;
      var minY = 99999;
      var maxY = -99999;


      for ( var i = 0; i < floats.length; i++ ) {

          if ( isOdd(i) ) { // the array of numbers is in the form (x,y,x,y,x,y,x...)

              // Check Y values
              if ( floats[i] < minY ) { minY = floats[i]; }
              if ( floats[i] > maxY ) { maxY = floats[i]; }

          } else {

              // Check X values
              if ( floats[i] < minX ) { minX = floats[i]; }
              if ( floats[i] > maxX ) { maxX = floats[i]; }

          }

      }



       console.log('minX = ' + minX + ', minY = ' + minY + ', maxX = ' + maxX + ', maxY = ' + maxY);


function isOdd(num) { return num % 2;}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM