forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvas.lcb
More file actions
127 lines (100 loc) · 4.2 KB
/
Copy pathcanvas.lcb
File metadata and controls
127 lines (100 loc) · 4.2 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
Copyright (C) 2014-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
-- Library modules add public handlers to the bottom of the message path.
--
/*
This simple library gives access to the LiveCode Builder canvas syntax from LiveCode Script by wrapping in a few simple handlers.
Example usage:
canvasCreate 100, 100
canvasSetColor 1, 0, 0, 1
canvasFillCircle 50, 50, 25
canvasApplyToImage "image 1"
canvasDestroy
This creates a canvas of size 100x100, fills a circle of radius 25 at
the centre, then copies the contents of the canvas to 'image 1' (which
must already exist).
Tags: Canvas Library
*/
library com.livecode.library.canvas
metadata version is "1.0.0"
metadata author is "LiveCode"
metadata title is "Canvas Library"
-- We need syntax from both canvas and engine built-in modules.
use com.livecode.canvas
use com.livecode.engine
-- We store the current canvas in a module-scope variable.
variable sCanvas as optional Canvas
/*
Summary: Creates a canvas of the given size for the other handlers to use.
pWidth: The width of the created canvas.
pHeight: The height of the created canvas.
Description:
Creates a canvas of the given size for the other handlers to use.
*/
public handler canvasCreate(in pWidth as Integer, in pHeight as Integer) returns nothing
-- Create a new canvas, this will release any previous canvas.
put a new canvas with size [ pWidth, pHeight ] into sCanvas
-- Set the paint to opaque black and fill the whole area.
set the paint of sCanvas to solid paint with color [0, 0, 0, 1]
fill rectangle path of rectangle [ 0, 0, pWidth, pHeight ] on sCanvas
end handler
/*
Summary: Destroys the canvas
Description:
Destroys the canvas by simply assigning nothing to the canvas variable.
*/
public handler canvasDestroy() returns nothing
put nothing into sCanvas
end handler
/*
Summary: Sets the current color of the canvas to the given RGBA value.
pRed: The red component of the color to set.
pGreen: The green component of the color to set.
pBlue: The blue component of the color to set.
pAlpha: The alpha value of the color to set.
Description:
Sets the current color of the canvas to the given RGBA value.
*/
public handler canvasSetColor(in pRed as Real, in pGreen as Real, in pBlue as Real, in pAlpha as Real) returns nothing
set the paint of sCanvas to solid paint with color [pRed, pGreen, pBlue, pAlpha]
end handler
/*
Summary: Draws a filled circle.
pX: The x-coordinate of the centre of the circle.
pY: The y-coordinate of the centre of the circle.
pRadius: The radius of the circle.
Description:
Fills a circle of the given radius at the given position in the canvas.
*/
public handler canvasFillCircle(in pX as Real, in pY as Real, in pRadius as Real) returns nothing
fill circle path centered at point [pX, pY] with radius pRadius on sCanvas
end handler
/*
Summary: Copies the current contents of the canvas to the specified image object.
pObjectId: A string which is an object chunk referring to an image.
Description:
This handler copies the current contents of the canvas to the specified image object.
*/
public handler canvasApplyToImage(in pObjectId as String) returns nothing
variable tObject as ScriptObject
-- Resolving the script object gets a weak reference to the specified
-- object and places it in 'the result'.
resolve script object pObjectId
put the result into tObject
-- Now set the width and height of the image object to that of our canvas
set property "width" of tObject to the pixel width of sCanvas
set property "height" of tObject to the pixel height of sCanvas
-- Finally set the image data of the image object.
set property "imageData" of tObject to the pixel data of sCanvas
end handler
end library