Skip to main content
matias.kiviniemi
Legend
December 3, 2014
Question

Drawing lines and converting to point values

  • December 3, 2014
  • 0 replies
  • 317 views

Hi,

Need to do some simple line drawing in a Javascript plugin and first did this based on docs

createPoint: function(x, y) {
   var unit_x = new UnitValue(x, "px")
   var unit_y = new UnitValue(y, "px")
   var coords = [unit_x.as("pt"), unit_y.as("pt")]

   var point = new PathPointInfo();
   point.anchor = coords
   point.leftDirection = coords
   point.rightDirection = coords
   point.kind = PointKind.CORNERPOINT

   return point

    },

    drawLine: function(x1, y1, x2, y2) {

   var startPoint = Photoshop.createPoint(x1, y1)
   var stopPoint = Photoshop.createPoint(x2, y2)

   var spi = new SubPathInfo()
   spi.closed = false
   spi.operation = ShapeOperation.SHAPEXOR
   spi.entireSubPath = [startPoint, stopPoint]
   var line = app.activeDocument.pathItems.add("Line", [spi])
   line.strokePath(ToolType.PENCIL)
   line.remove()

    },

Now this misaligns the lines, i.e. coordinates are not where they should. The px->pt conversion should be OK at least from settings. I.e. I have PS set to 72dpi and result is pt=px. If I manually scale by 72/96, it almost aligns but not quite. So sounds like there is some transform/scale in place that might be doc specific that should be applied. Any thoughts?

More searching found me this that is working, but I'd prefer to use just SDK where I can..

drawLine: function (x1, y1, x2, y2, width) { 
   var desc = new ActionDescriptor() 
   var lineDesc = new ActionDescriptor() 
  
   var startDesc = new ActionDescriptor() 
   startDesc.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), x1 ) 
   startDesc.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), y1 ) 
   lineDesc.putObject( charIDToTypeID('Strt'), charIDToTypeID('Pnt '), startDesc ) 

   var endDesc = new ActionDescriptor() 
   endDesc.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), x2 ) 
   endDesc.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), y2 ) 

   lineDesc.putObject( charIDToTypeID('End '), charIDToTypeID('Pnt '), endDesc ) 
   lineDesc.putUnitDouble( charIDToTypeID('Wdth'), charIDToTypeID('#Pxl'), width || 1 ) 
   desc.putObject( charIDToTypeID('Shp '), charIDToTypeID('Ln  '), lineDesc ) 
   desc.putBoolean( charIDToTypeID('AntA'), true ) 
   executeAction( charIDToTypeID('Draw'), desc, DialogModes.NO ) 

    }

This topic has been closed for replies.