Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Path Shape getSize returns 0 width 0 height #525

Open
jamespacileo opened this issue Jul 13, 2013 · 2 comments
Open

Path Shape getSize returns 0 width 0 height #525

jamespacileo opened this issue Jul 13, 2013 · 2 comments
Labels

Comments

@jamespacileo
Copy link

The shape is unaware of its bounding box, makes caching Path Shapes really hard.

@agamemnus
Copy link

I need the same functionality.. :-(

@agamemnus
Copy link

I found a StackOverflow reply that says essentially it's just not coded.

I've created a workaround which might help you:

  1. First, I convert the path shape to an image, picking a reasonable buffer for the shape.
  2. Then, I trim empty spaces with this function that I made:
function canvas_trim_empty_space (source_canvas) {
 var canvas_initial_width = source_canvas.width, canvas_initial_height = source_canvas.height
 var ctx = source_canvas.getContext ('2d')
 var canvas_data = ctx.getImageData(0, 0, canvas_initial_width, canvas_initial_height)
 var canvas_data_data = canvas_data.data
 var curlen = canvas_data_data.length
 var x0=0, x1=canvas_initial_width, y0=0, y1=canvas_initial_height
 for (var y = 0; y < canvas_initial_height; y++) {
  var y_address = y*canvas_initial_width
  var all_empty = true
  for (var x = 0; x < canvas_initial_width; x++) {
   var a = canvas_data_data[(y_address + x)*4+3]
   if (a != 0) {all_empty = false; break}
  }
  if (all_empty == false) {y0 = y; break}
 }
 for (var y = canvas_initial_height - 1; y >= 0; y--) {
  var y_address = y*canvas_initial_width
  var all_empty = true
  for (var x = 0; x < canvas_initial_width; x++) {
   var a = canvas_data_data[(y_address + x)*4+3]
   if (a != 0) {all_empty = false; break}
  }
  if (all_empty == false) {y1 = y; break}
 }
 for (var x = 0; x < canvas_initial_width; x++) {
  var all_empty = true
  for (var y = 0; y < canvas_initial_height; y++) {
   var a = canvas_data_data[(y*canvas_initial_width + x)*4+3]
   if (a != 0) {all_empty = false; break}
  }
  if (all_empty == false) {x0 = x; break}
 }
 for (var x = canvas_initial_width - 1; x >= 0; x--) {
  var all_empty = true
  for (var y = 0; y < canvas_initial_height; y++) {
   var a = canvas_data_data[(y*canvas_initial_width + x)*4+3]
   if (a != 0) {all_empty = false; break}
  }
  if (all_empty == false) {x1 = x; break}
 }
 var new_width = x1-x0+1, new_height = y1-y0+1
 var target_canvas = document.createElement ('canvas')
 var target_ctx = target_canvas.getContext ('2d')
 target_canvas.width  = new_width
 target_canvas.height = new_height
 target_ctx.drawImage (source_canvas, x0, y0, new_width, new_height, 0, 0, new_width, new_height)
 return target_canvas
}

@lavrton lavrton added the bug label Feb 27, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants