File: clip_path.rb

package info (click to toggle)
ruby-rmagick 6.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,232 kB
  • sloc: cpp: 19,563; ruby: 17,147; sh: 88; javascript: 36; makefile: 13
file content (60 lines) | stat: -rw-r--r-- 1,244 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
require 'rmagick'

points = [
  145, 65, 174, 151, 264, 151, 192, 205,
  218, 291, 145, 240, 72, 291, 98, 205,
  26, 151, 116, 151
]

pr = Magick::Draw.new

# Define a clip-path.
# The name of the clip-path is "example"
pr.define_clip_path('example') do
  pr.polygon(*points)
end

# Enable the clip-path
pr.push
pr.clip_path('example')

# Composite the Flower Hat image over
# the background using the clip-path
girl = Magick::ImageList.new
girl.read('images/Flower_Hat.jpg')

cols = rows = nil

# Our final image is about 280 pixels wide, so here
# we widen our picture to fit. The change_geometry
# method will adjust the height proportionately.

girl.change_geometry('280') do |c, r|
  pr.composite(0, 0, c, r, girl)
  cols = c
  rows = r
end

pr.pop

# Create a canvas to draw on, a bit bigger than the star.
canvas = Magick::Image.new(cols, rows)

star = Magick::Draw.new
star.stroke('gray50')
star.fill('gray50')
points.map! { |p| p + 8 }
star.polygon(*points)
star.draw(canvas)
canvas = canvas.blur_image(0, 3)

# Draw the star shadow over the background
pr.draw(canvas)

# Crop away all the solid white border pixels.
crop = canvas.bounding_box
canvas.crop!(crop.x, crop.y, crop.width, crop.height)

canvas.write('clip_path.gif')

exit