Method Summary | |
Painter() | Creates Painter object |
setDrawArea(canvas,options) | Associates a <canvas> element with the drawing area. |
setImageSize(width,height) | Sets image size |
addControlPanel | Adds a panel containing control buttons |
loadImage(url) | Loads an image for editing |
setImage(image) | Sets an image for editing |
loadBackgroundImage(url) | Loads the background image |
setBackgroundImage(image) | Sets the background image |
loadMaskImage(url) | Loads the mask image |
setMaskImage(image) | Sets the mask image |
getImageData(options) | Returns image Data |
getSelectedData() | Returns Data of the selected portion of the image |
isChanged() | Returns true if the user has changed anything on the draw area |
setTool(tool) | Sets current drawing tool |
setColor(color) | Sets current color |
setPen(pen) | Sets current pen |
setPattern(pen) | Sets current pattern |
setFont(font) | Sets current font for the text tool |
doAction(action) | Executes an action |
putClipart(url,x,y,options) | Puts a clipart on the drawing area |
addCustomAction(name,handler,icons) | Defines custom action button |
addCustomTool(name,params) | Defines custom tool button |
addCustomPen(name,params) | Defines custom pen |
addCustomPattern(name,image,replacable_color) | Defines custom pattern |
setIcons(icons) | Specfies custom icons for the buttons |
Method Detail |
Painter
var painter = new Painter();
setDrawArea(canvas,options)
canvas
- an HTML5 canvas element to serve as the drawing areaoptions
- optional parameters for the drawing area.
The following options are supported:select_ignore_bgcolor
- if true
the tool select_rect
doesn't copy the background color.smooth_curve
- turns on/off smoothing for curve and area tools.
Specify true
or false
. The default is true
;
painter.setDrawArea(document.getElementById("drawarea")), { background : "#ffffff" });NOTE: If you use jQuery to access DOM elements remember that jQuery selectors, e.g.
$("#drawarea")
return jQuery wrapping object, not a DOM element, so you should pass $("#drawarea")[0]
instead.
setImageSize(width,height)
If the image size exceeds the canvas size the use can scroll it using pan
tool.
If you load an existing image or the background one the size is automatically set according to them. If you call this method after loading an image the size is changed to the specified one. If you neither load anything nor call this method the image size is set by the draw area canvas.
width
- image widthheight
- image height.addControlPanel(canvas,options)
Tool Panel
or
Color Panel
or
Pen Panel
or
Action Panel
.
Every panel holds the buttons specified by the option
tools
or
colors
or
pens
or
actions
accordingly.
canvas
- An HTML5 canvas element that serves as the button panel.
The canvas size can be set according to your desing preferences.options
- The panel options object. The only required option field
is the list of buttons. Its name defines the panel type and can be one of the following:
"tools" or "colors",or "pens", or "actions."
The option value must be comma separated list of button names.
rows
- number of rows to arrange the buttonscols
- number of columns to arrange the buttonshgap
- horizontal gap between the buttonsvgap
- vertical gap between the buttonsbackground
- the background color of the panelpainter.addControlPanel(document.getElementById("colorpanel")), { colors : "#000000,#ff0000,#00ff00,#0000ff,#ffffff,#ffff00,#00ffff,#ff00ff", rows : 2, cols : 4, background : "#c0c0c0" });
You can create more then one panel of same type to arrange buttons in separate groups on the page.
NOTE: If you use jQuery to access DOM elements remember that jQuery selectors, e.g. $("#colorpanel")
return jQuery wrapping object, not a DOM element, so you should pass $("#colorpanel")[0]
instead.
getImageData(options)
options
- the following options are supported:
width
- specifies to scale the returned image width to the specified valueheight
- specifies to scale the returned image height to the specified valuebackground
- if true
the background layer is painted on the returned image.drawing
- if true
the drawing layer is painted on the returned image.format
- specifies the image format, for example: image/png
or image/jpeg
.
Supported formats depend on the browser.
Only image/png
is supported in all browsersdata:image/png;base64,<...data...>
getSelectedData()
null
if there is no selection.x
- x left side of the selection,y
- y upper side of the selection,data
- string containing image data of the selected portion in Data URI format:data:image/png;base64,<...data...>
loadImage(url)
url
- URL of the image to be loaded.
The URL can be either absolute or relative to the current document.setImage(image)
image
- Image object to be set.
It can be either JavaScript Image
or an <img>
element.
loadBackgroundImage(url)
url
- URL of the background image.
The URL can be either absolute or relative to the current document.setBackgroundImage(image)
image
- An Image object to be set.
It can be either JavaScript Image
or an <img>
element.
loadMaskImage(url)
url
- URL of the mask image.
The URL can be either absolute or relative to the current document.setMaskImage(image)
image
- An Image object to be used as the mask.
It can be either JavaScript Image
or an <img>
element.
isChanged()
true
if the user has changed anything on the draw area.setTool(tool)
tool
- The tool name. Currently the following tools are supported:
line,arrow_path,curve,rect,round_rect,oval,polyline,color_picker,move_rect,bezier,pan, arrow,arrow_dim,area,filled_rect,filled_round_rect,filled_oval,polygon,text,copy_rect, eraser,select_rect
setColor(color)
color
- color specified as:
"#RRGGBB"
or "#RGB"
or "rgba(red,green,blue,alpha)"
.
setPen(pen)
pen
- The pen name. Currently the following pens are supported:
solid1,solid2,solid4,dashed1,dashed2,dashed4,zigzag2,zigzag4
setPattern(pattern)
pen
- The pattern name. Currently the following pens are supported:
solid, hatching, crosshatch, dots, grid, saw, bricks, wave
setFont(font)
font
- Font specification in the following format:
name-style-size
, where style can be one of the following: plain, bold, italic, bolditalic
doAction(action)
action
- Action name. The action name can be
clear, undo, redo, font
,putClipart(url,x,y,options)
url
- URL of the image.x
- horizontal coordinate of the clipart (specified relative to the DrawArea element).y
- vertical coordinate of the clipart (specified relative to the DrawArea element).options
- Options object. The following object fields are supported:
canresize
- if set to false
the user can't resize the clipart before applying it; the default is true
.canrotate
- if set to false
the user can't rotate the clipart before applying it; the default is true
.opaque
- if set to false
the user can't capture the clipart on its transparent areas; the default is true
.addCustomAction(name,handler,icon)
name
- name of the buttonhandler
- the function to invoke when the button is pressedicon
- URL of the button iconpainter.addCustomAction('help',function(action){ window.open('help.html','help','width=400, height=300'); }, 'help.gif' );
addCustomTool(name,params)
name
- name of the tool params
- object defining the fields: tool
,
color
, pen
, pattern
, icon
. Every field is optional but the tool
.
For any unspecified field current setting is used.
painter.addCustomTool('river', { tool:'curve', color:'#0000FF', pen:'solid4' icon:'river.gif' });
addCustomPen(name,params)
name
- name of the pen params
- object defining the fields: type
,
path
, advance
, width
.
See more details about custom pens.
addCustomPattern(name,pattern_image,replacable_color)
name
- name of the patternpattern_icon
- image which is used as the patternreplacable_color
- if specified the editor replaces the color on the image with the current colorpainter.addCustomPattern('bush', 'patterns/bush.gif', '#000000' );
setIcons(icons)
icons
- An object whose fields specify new icons. The field names must correspond the tool or pen or action names
you want to replace icons for. The corresponding values must be the icons URLs. You can replace either some or all the icons.
painter.setIcons({ clear : 'my_clear_image.gif', undo : 'my_undo_image.gif', redo : 'my_redo_image.gif' });