-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # LICENSE
- Loading branch information
Showing
9 changed files
with
101 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "StickyGrid", | ||
"commands": [ | ||
{ | ||
"script": "script.cocoascript", | ||
"handler": "snapToGrid", | ||
"shortcut": "ctrl cmd g", | ||
"name": "Snap to Grid", | ||
"identifier": "snaptogrid" | ||
} | ||
], | ||
"menu": { | ||
"items": [ | ||
"snaptogrid" | ||
], | ||
"title": "StickyGrid" | ||
}, | ||
"identifier": "co.randompaper.sketch.stickygrid", | ||
"version": "2.0.0", | ||
"description": "Sketch plugin to make paths be snapped to grid.", | ||
"author": "Yasuaki Uechi", | ||
"authorEmail": "uetchy@randompaper.co", | ||
"license": "MIT", | ||
"homepage": "https://github.com/uetchy/Sketch-StickyGrid", | ||
"keywords": [ | ||
"grid" | ||
] | ||
} |
62 changes: 62 additions & 0 deletions
62
StickyGrid.sketchplugin/Contents/Sketch/script.cocoascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
function computePosition(pos, intval) { | ||
return Math.round(pos / intval) * intval; | ||
} | ||
|
||
function adjustPointsTo(shapeLayer, gridInterval) { | ||
var path = shapeLayer.path(); | ||
var pointsCount = path.numberOfPoints(); | ||
var artboardFrame = shapeLayer.frameInArtboard(); | ||
|
||
for (var j=0; j < pointsCount; j++) { | ||
var point = path.pointAtIndex(j); | ||
|
||
var absolutePoint = shapeLayer.absolutePoint(point.point()); | ||
|
||
var relX = computePosition( | ||
artboardFrame.origin.x + absolutePoint.x, | ||
gridInterval) - (artboardFrame.origin.x + absolutePoint.x | ||
); | ||
|
||
var relY = computePosition( | ||
artboardFrame.origin.y + absolutePoint.y, | ||
gridInterval) - (artboardFrame.origin.y + absolutePoint.y); | ||
|
||
var cgPoint = shapeLayer.relativePoint( | ||
CGPointMake( | ||
absolutePoint.x + relX, | ||
absolutePoint.y + relY | ||
)); | ||
|
||
point.movePointTo(cgPoint); | ||
} | ||
|
||
shapeLayer.adjustFrameAfterEdit(); | ||
} | ||
|
||
function snapToGrid(context) { | ||
var app = [NSApplication sharedApplication]; | ||
var doc = context.document; | ||
var selection = context.selection; | ||
var gridInterval = doc.grid().gridSize(); | ||
|
||
for (var i=0; i < [selection count]; i++) { | ||
var object = [selection objectAtIndex: i]; | ||
|
||
if ([object isKindOfClass:[MSShapePathLayer class]]) { | ||
// MSShapeGroup | ||
adjustPointsTo(object, gridInterval); | ||
} else if ([object isMemberOfClass:[MSLayerGroup class]]) { | ||
// MSLayerGroup | ||
for (var l=0; l < [[object layers] count]; l++) { | ||
var shapeLayerGroup = [[object layers] objectAtIndex:l]; | ||
|
||
for (var l2=0; l2 < [[shapeLayerGroup layers] count]; l2++) | ||
adjustPointsTo([[shapeLayerGroup layers] objectAtIndex:l2], gridInterval); | ||
} | ||
} else { | ||
// MSShapePathLayer | ||
for (var l=0; l < [[object layers] count]; l++) | ||
adjustPointsTo([[object layers] objectAtIndex:l], gridInterval); | ||
} | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
This file was deleted.
Oops, something went wrong.