diff --git a/CHANGELOG.md b/CHANGELOG.md index 2331395..5675afe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ Squib follows [semantic versioning](http://semver.org). ## v0.9.0 / Unreleased Features: -* Crop your PNGs! This means you can work from spritesheets if you want. New options to `png` are documented in the API docs and in the `load_images.rb` sample. (#11) +* Crop your PNGs and SVGs! This means you can work from spritesheets if you want. New options to `png` and `svg` are documented in the API docs and demonstrated in the `load_images.rb` sample. (#11) Chores: * Ripped out a lot of old constants used from the old way we handled arguments. Yay negative churn! diff --git a/lib/squib/api/image.rb b/lib/squib/api/image.rb index 88ff0e9..1369490 100644 --- a/lib/squib/api/image.rb +++ b/lib/squib/api/image.rb @@ -72,6 +72,13 @@ module Squib # @option opts blend [:none, :multiply, :screen, :overlay, :darken, :lighten, :color_dodge, :color_burn, :hard_light, :soft_light, :difference, :exclusion, :hsl_hue, :hsl_saturation, :hsl_color, :hsl_luminosity] (:none) the composite blend operator used when applying this image. See Blend Modes at http://cairographics.org/operators. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion} # @option opts angle [FixNum] (0) Rotation of the in radians. Note that this rotates around the upper-left corner, making the placement of x-y coordinates slightly tricky. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion} # @option opts mask [String] (nil) If specified, the image will be used as a mask for the given color/gradient. Transparent pixels are ignored, opaque pixels are the given color. Note: the origin for gradient coordinates is at the given x,y, not at 0,0 as it is most other places. + # @option opts crop_x [Integer] (0) Crop the loaded image at this x coordinate. Supports Unit Conversion, see {file:README.md#Units Units}. + # @option opts crop_y [Integer] (0) Crop the loaded image at this y coordinate. Supports Unit Conversion, see {file:README.md#Units Units}. + # @option opts crop_corner_radius [Integer] (0): Radius for rounded corners, both x and y. When set, overrides crop_corner_x_radius and crop_corner_y_radius. Supports Unit Conversion, see {file:README.md#Units Units}. + # @option opts crop_corner_x_radius [Integer] (0): x radius for rounded corners of cropped image. Supports Unit Conversion, see {file:README.md#Units Units}. + # @option opts crop_corner_y_radius [Integer] (0): y radius for rounded corners of cropped image. Supports Unit Conversion, see {file:README.md#Units Units}. + # @option opts crop_width [Integer] (:native): Width of the cropped image. Supports Unit Conversion, see {file:README.md#Units Units}. + # @option opts crop_height [Integer] (:native): Height of the cropped image. Supports Unit Conversion, see {file:README.md#Units Units}. # @return [nil] Returns nil # @api public def svg(opts = {}) diff --git a/lib/squib/graphics/image.rb b/lib/squib/graphics/image.rb index 76d23f2..4849ff2 100644 --- a/lib/squib/graphics/image.rb +++ b/lib/squib/graphics/image.rb @@ -31,7 +31,6 @@ module Squib cc.rotate(trans.angle) cc.translate(-box.x, -box.y) - # cc.translate(trans.crop_x, trans.crop_y) trans.crop_width = png.width.to_f if trans.crop_width == :native trans.crop_height = png.height.to_f if trans.crop_height == :native cc.rounded_rectangle(box.x, box.y, trans.crop_width, trans.crop_height, trans.crop_corner_x_radius, trans.crop_corner_y_radius) @@ -67,6 +66,13 @@ module Squib cc.translate(box.x, box.y) cc.rotate(trans.angle) cc.scale(scale_width, scale_height) + + trans.crop_width = box.width if trans.crop_width == :native + trans.crop_height = box.height if trans.crop_height == :native + cc.rounded_rectangle(0, 0, trans.crop_width / scale_width, trans.crop_height / scale_height, trans.crop_corner_x_radius, trans.crop_corner_y_radius) + cc.clip + cc.translate(-trans.crop_x, -trans.crop_y) + cc.operator = paint.blend unless paint.blend == :none if paint.mask.to_s.empty? cc.render_rsvg_handle(svg, svg_args.id) diff --git a/samples/load_images.rb b/samples/load_images.rb index 545ef66..c781627 100644 --- a/samples/load_images.rb +++ b/samples/load_images.rb @@ -30,6 +30,10 @@ Squib::Deck.new(width: 825, height: 1125, cards: 1) do crop_x: 64, crop_y: 0, crop_corner_x_radius: 25, crop_corner_y_radius: 25, crop_width: 64, crop_height: 64, angle: Math::PI / 6 + # Cropping also works on SVGs too + svg file: 'spanner.svg', x: 300, y: 500, width: 64, height: 64, + crop_x: 32, crop_y: 32, crop_width: 32, crop_height:32 + # We can also limit our rendering to a single object, if the SVG ID is set svg file: 'spanner.svg', id: '#backdrop', x: 50, y: 350, width: 75, height: 75 # Squib prepends a #-sign if one is not specified