Skip to content

Commit

Permalink
Merge branch 'sketch-3.3'
Browse files Browse the repository at this point in the history
# Conflicts:
#	LICENSE
  • Loading branch information
uetchy committed Dec 14, 2015
2 parents 6f95191 + c68417c commit ed3fea5
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 80 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 Yasuaki Uechi
Copyright (c) 2015 Yasuaki Uechi http://randompaper.co

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

Sketch plugin to make paths be snapped to grid.

![](https://raw.githubusercontent.com/uetchy/Sketch-StickyGrid/master/assets/readme_images/stickygrid.gif)
![](https://raw.githubusercontent.com/uetchy/Sketch-StickyGrid/master/Contents/Resources/readme_images/stickygrid.gif)

## Installation

1. [Download the plugin](https://github.com/uetchy/Sketch-StickyGrid/archive/master.zip)
2. Unzip the archive
3. Place the folder into your Sketch Plugins folder.
2. Unzip the archive and double-click `StickyGrid.sketchplugin`

If you prefer CLI-way:

```
$ cd $HOME/Library/Application Support/com.bohemiancoding.sketch3/Plugins
$ git clone https://github.com/uetchy/Sketch-StickyGrid.git
```

## Usage

Expand Down
63 changes: 0 additions & 63 deletions Snap to Grid.sketchplugin

This file was deleted.

28 changes: 28 additions & 0 deletions StickyGrid.sketchplugin/Contents/Sketch/manifest.json
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 StickyGrid.sketchplugin/Contents/Sketch/script.cocoascript
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 removed assets/readme_images/stickygrid.gif
Binary file not shown.
Binary file removed assets/readme_images/stickygrid_2.gif
Binary file not shown.
13 changes: 0 additions & 13 deletions chest.json

This file was deleted.

0 comments on commit ed3fea5

Please sign in to comment.