├── .gitattributes ├── .gitignore ├── LICENSE ├── Makefile ├── README.md └── StickyGrid.sketchplugin └── Contents └── Sketch ├── manifest.json └── script.js /.gitattributes: -------------------------------------------------------------------------------- 1 | *.cocoascript linguist-language=JavaScript 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Yasuaki Uechi https://randompaper.co 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | PLUGIN_NAME = $(notdir $(CURDIR)) 2 | PLUGIN_DIR = $(HOME)/Library/Application Support/com.bohemiancoding.sketch3/Plugins/$(PLUGIN_NAME) 3 | 4 | link: 5 | ln -s "$(CURDIR)" "$(PLUGIN_DIR)" 6 | 7 | unlink: 8 | rm "$(PLUGIN_DIR)" 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # StickyGrid 2 | 3 | [![Join the chat at https://gitter.im/uetchy/Sketch-StickyGrid](https://badges.gitter.im/uetchy/Sketch-StickyGrid.svg)](https://gitter.im/uetchy/Sketch-StickyGrid?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 4 | 5 | Sketch plugin to make paths be snapped to Grid and/or Pixel Grid. 6 | 7 | ![](http://uechi-public.s3.amazonaws.com/github/sketch-stickygrid.png) 8 | 9 | ## Installation 10 | 11 | 1. [Download the plugin](https://github.com/uetchy/Sketch-StickyGrid/archive/master.zip) 12 | 2. Unzip the archive and double-click `StickyGrid.sketchplugin` 13 | 14 | If you prefer CLI-way: 15 | 16 | ``` 17 | $ cd "$HOME/Library/Application Support/com.bohemiancoding.sketch3/Plugins" 18 | $ git clone https://github.com/uetchy/Sketch-StickyGrid.git 19 | ``` 20 | 21 | ## Usage 22 | 23 | ### Snap to Grid `ctrl` + `⌘` + `G` 24 | 25 | ### Snap to Pixel Grid `ctrl` + `⌘` + `X` 26 | 27 | ## More information 28 | 29 | See official document [Sketch - Pixel Precision](https://www.sketchapp.com/learn/documentation/13-other/5-pixel-precision.html). 30 | 31 | ## Contributing 32 | 33 | ### Debugging 34 | 35 | #### Dump classes 36 | 37 | ``` 38 | class-dump /Applications/Sketch.app > sketch.dump 39 | ``` 40 | 41 | #### Log 42 | 43 | ```js 44 | log(something.treeAsDictionary()); 45 | ``` 46 | 47 | to see: 48 | 49 | ``` 50 | tail -f /var/log/system.log 51 | ``` 52 | -------------------------------------------------------------------------------- /StickyGrid.sketchplugin/Contents/Sketch/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "StickyGrid", 3 | "commands": [{ 4 | "script": "script.js", 5 | "handler": "snapToGrid", 6 | "shortcut": "ctrl cmd g", 7 | "name": "Snap to Grid", 8 | "identifier": "snapToGrid" 9 | }, { 10 | "script": "script.js", 11 | "handler": "snapToPixelGrid", 12 | "shortcut": "ctrl cmd x", 13 | "name": "Snap to Pixel Grid", 14 | "identifier": "snapToPixelGrid" 15 | }], 16 | "menu": { 17 | "items": [ 18 | "snapToGrid", 19 | "snapToPixelGrid" 20 | ], 21 | "title": "StickyGrid" 22 | }, 23 | "identifier": "co.randompaper.sketch.stickygrid", 24 | "version": "3.0.0", 25 | "description": "Sketch plugin to make paths be snapped to grid.", 26 | "author": "Yasuaki Uechi", 27 | "authorEmail": "uetchy@randompaper.co", 28 | "license": "MIT", 29 | "homepage": "https://github.com/uetchy/Sketch-StickyGrid", 30 | "keywords": [ 31 | "grid" 32 | ] 33 | } 34 | -------------------------------------------------------------------------------- /StickyGrid.sketchplugin/Contents/Sketch/script.js: -------------------------------------------------------------------------------- 1 | function computePosition(pos, gridInterval) { 2 | return Math.round(pos / gridInterval) * gridInterval; 3 | } 4 | 5 | // Adjust point's coords of shape layers to grid 6 | function adjustPointsOf(shapeLayer, gridInterval) { 7 | var path = shapeLayer.path(); 8 | var pointsCount = path.numberOfPoints(); 9 | var artboardFrame = shapeLayer.frameInArtboard(); 10 | 11 | for (var j = 0; j < pointsCount; j++) { 12 | var point = path.pointAtIndex(j); 13 | 14 | var absolutePoint = shapeLayer.absolutePoint(point.point()); 15 | 16 | var absX = artboardFrame.origin.x + absolutePoint.x; 17 | var relX = computePosition(absX, gridInterval) - absX; 18 | 19 | var absY = artboardFrame.origin.y + absolutePoint.y; 20 | var relY = computePosition(absY, gridInterval) - absY; 21 | 22 | var cgPoint = shapeLayer.relativePoint( 23 | CGPointMake( 24 | absolutePoint.x + relX, 25 | absolutePoint.y + relY 26 | )); 27 | 28 | point.movePointTo(cgPoint); 29 | } 30 | 31 | shapeLayer.adjustFrameAfterEditIntegral(false); 32 | } 33 | 34 | function adjustSelection(selection, gridInterval) { 35 | for (var i = 0; i < selection.count(); i++) { 36 | var object = selection.objectAtIndex(i); 37 | 38 | if (object.isKindOfClass(MSShapePathLayer.class())) { 39 | // MSShapeGroup 40 | adjustPointsOf(object, gridInterval); 41 | } else if (object.isMemberOfClass(MSLayerGroup.class())) { 42 | // MSLayerGroup 43 | for (var l = 0; l < object.layers().count(); l++) { 44 | var shapeLayerGroup = object.layers().objectAtIndex(l); 45 | 46 | for (var l2 = 0; l2 < shapeLayerGroup.layers().count(); l2++) 47 | adjustPointsOf(shapeLayerGroup.layers().objectAtIndex(l2), gridInterval); 48 | } 49 | } else { 50 | // MSShapePathLayer 51 | for (var l = 0; l < object.layers().count(); l++) 52 | adjustPointsOf(object.layers().objectAtIndex(l), gridInterval); 53 | } 54 | } 55 | } 56 | 57 | function snapToGrid(context) { 58 | var gridInterval = MSDefaultGrid.defaultGrid().gridSize(); 59 | adjustSelection(context.selection, gridInterval); 60 | } 61 | 62 | function snapToPixelGrid(context) { 63 | adjustSelection(context.selection, 1.0); 64 | } 65 | --------------------------------------------------------------------------------