Class PaintWeb
The PaintWeb application object.
Defined in: paintweb.js.
Constructor Attributes | Constructor Name and Description |
---|---|
PaintWeb(win, doc)
|
Field Attributes | Field Name and Description |
---|---|
<static> |
PaintWeb.baseFolder
PaintWeb base folder.
|
Holds the buffer canvas and context references.
|
|
PaintWeb build date (YYYYMMDD).
|
|
Image in the clipboard.
|
|
Holds all the PaintWeb commands.
|
|
Holds all the PaintWeb configuration.
|
|
<private> |
The document element PaintWeb is working with.
|
<constant> |
Element node type constant.
|
<private> |
Holds references to DOM elements.
|
Custom application events object.
|
|
Holds all the PaintWeb extensions.
|
|
The graphical user interface object instance.
|
|
<private> |
The image history.
|
Holds image information: width, height, zoom and more.
|
|
<static> <constant> |
PaintWeb.INIT_DONE
Application initialization completed successfully.
|
<static> <constant> |
PaintWeb.INIT_ERROR
Application initialization failed.
|
<static> <constant> |
PaintWeb.INIT_NOT_STARTED
Application initialization not started.
|
<static> <constant> |
PaintWeb.INIT_STARTED
Application initialization started.
|
Application initialization state.
|
|
<private> <inner> |
Holds the keyboard event listener object.
|
Holds all language strings used within PaintWeb.
|
|
Holds the current layer ID, canvas and context references.
|
|
<private> |
Holds the last recorded mouse coordinates and the button state (if it's
down or not).
|
Resolution information.
|
|
Tells if the current tool allows the drawing of shadows.
|
|
Tells if the browser supports the Canvas Shadows API.
|
|
<private> |
List of Canvas context properties to save and restore.
|
<private> <inner> |
Holds temporary state information during PaintWeb initialization.
|
The instance of the active tool object.
|
|
Unique ID for the current PaintWeb instance.
|
|
PaintWeb version.
|
|
<private> |
The window object PaintWeb is working with.
|
Method Attributes | Method Name and Description |
---|---|
clipboardPaste(ev)
Paste the current clipboard image.
|
|
commandRegister(id, func)
Register a new command in PaintWeb.
|
|
Unregister a command from PaintWeb.
|
|
<private> |
The
configChange application event handler. |
<private> |
Asynchronously load the configuration file.
|
<private> |
configReady(xhr)
The configuration reader.
|
destroy()
Destroy a PaintWeb instance.
|
|
<private> |
ev_canvas(ev)
The Canvas events handler.
|
<private> |
ev_keyboard(ev)
The global keyboard events handler.
|
<private> |
ev_numberInput(ev)
This is the
keypress event handler for inputs of type=number. |
<private> |
The
load event handler for each extension script. |
Register a new extension into PaintWeb.
|
|
Unregister an extension from PaintWeb.
|
|
<private> |
guiLoad()
Load th PaintWeb GUI.
|
<private> |
guiMarkupReady(xhr)
The GUI markup reader.
|
<private> |
The
load event handler for the PaintWeb GUI script. |
Add the current image layer to the history.
|
|
historyGoto(pos)
Jump to any ImageData/position in the history.
|
|
Perform action redo.
|
|
<private> |
Clear the image history.
|
Perform action undo.
|
|
imageClear(ev)
Clear the image.
|
|
imageCrop(cropX, cropY, cropWidth, cropHeight)
Crop the image.
|
|
imageLoad(importImage)
Load an image.
|
|
imageSave(type)
Save the image.
|
|
<private> |
The
imageSaveResult application event handler. |
imageZoomIn(ev)
Zoom into the image.
|
|
imageZoomOut(ev)
Zoom out of the image.
|
|
imageZoomReset(ev)
Reset the image zoom level to normal.
|
|
imageZoomTo(level)
Change the image zoom level.
|
|
init(handler)
Initialize PaintWeb.
|
|
<private> |
Initialize the Canvas elements.
|
<private> |
Initialize the PaintWeb commands.
|
<private> |
Initialization procedure which runs after the configuration, language and
GUI files have loaded.
|
<private> |
Initialize the Canvas buffer context.
|
<private> |
initError(msg)
Report an initialization error.
|
<private> |
Load all the extensions.
|
<private> |
Load all the configured drawing tools.
|
<private> |
The
load event handler for the JSON library script. |
<private> |
langLoad()
Asynchronously load the language file.
|
<private> |
langReady(xhr)
The language file reader.
|
Update the current image layer by moving the pixels from the buffer onto
the layer.
|
|
<private> <inner> |
preInit()
PaintWeb pre-initialization code.
|
<private> |
The
load event handler for the PaintWeb library script. |
scriptLoad(url, handler)
Load a script into the document.
|
|
selectAll(ev)
Select all the pixels.
|
|
selectionCopy(ev)
Copy the available selection.
|
|
selectionCut(ev)
Cut the available selection.
|
|
Allow shadows.
|
|
Disallow shadows.
|
|
stateRestore(context, stateObj)
Restore the state of a Canvas context.
|
|
stateSave(context)
Save the state of a Canvas context.
|
|
styleLoad(id, url, media, handler)
Insert a stylesheet into the document.
|
|
Swap the fill and stroke styles.
|
|
toolActivate(id, ev)
Activate a drawing tool by ID.
|
|
<private> |
The
load event handler for each tool script. |
toolRegister(id)
Register a new drawing tool into PaintWeb.
|
|
toolSnapXY(x, y)
Perform horizontal/vertical line snapping.
|
|
toolUnregister(id)
Unregister a drawing tool from PaintWeb.
|
|
toString()
|
|
Update the canvas scaling.
|
- Parameters:
- {Window} win Optional, Default: window
- The window object to use.
- {Document} doc Optional, Default: document
- The document object to use.
- See:
- PaintWeb#commandRegister Register a new command.
- PaintWeb#commandUnregister Unregister a command.
- Default Value:
- document
- See:
- PaintWeb#extensionRegister Register a new extension.
- PaintWeb#extensionUnregister Unregister an extension.
- PaintWeb.config.extensions Holds the list of extensions to be loaded automatically when PaintWeb is initialized.
- PaintWeb.INIT_NOT_STARTED - The initialization is not started.
- PaintWeb.INIT_STARTED - The initialization process is running.
- PaintWeb.INIT_DONE - The initialization process has completed successfully.
- PaintWeb.INIT_ERROR - The initialization process has failed.
- Default Value:
- PaintWeb.INIT_NOT_STARTED
- See:
- pwlib.dom.KeyboardEventListener The class dealing with the cross-browser differences in the DOM keyboard events.
- Default Value:
- true
- Default Value:
- true
When the Canvas is resized the state is lost. Using context.save/restore state does work only in Opera. In Firefox/Gecko and WebKit saved states are lost after resize, so there's no state to restore. As such, PaintWeb has its own simple state save/restore mechanism. The property values are saved into a JavaScript object.
- See:
- PaintWeb#stateSave to save the canvas context state.
- PaintWeb#stateRestore to restore a canvas context state.
- See:
- PaintWeb.config.toolDefault holds the ID of the tool which is activated when the application loads.
- PaintWeb#toolActivate Activate a drawing tool by ID.
- PaintWeb#toolRegister Register a new drawing tool.
- PaintWeb#toolUnregister Unregister a drawing tool.
- pwlib.tools holds the drawing tools.
- Default Value:
- window
- Parameters:
- {Event} ev Optional
- The DOM Event object which generated the request.
- Returns:
- {Boolean} True if the operation was successful, or false if not.
- See:
- The command implementation.
Once the command is successfully registered, this method dispatches the pwlib.appEvent.commandRegister application event.
- Parameters:
- {String} id
- The ID of the new command.
- {Function} func
- The command function.
- Returns:
- {Boolean} True if the command was successfully registered, or false if not.
- See:
- PaintWeb#commandUnregister allows you to unregister commands.
- PaintWeb#commands Holds all the registered commands.
Before the command is unregistered, this method dispatches the pwlib.appEvent.commandUnregister application event.
- Parameters:
- {String} id
- The ID of the command you want to unregister.
- Returns:
- {Boolean} True if the command was removed successfully, or false if not.
- See:
- PaintWeb#commandRegister allows you to register new commands.
- PaintWeb#commands Holds all the registered commands.
configChange
application event handler. This method
updates the Canvas context properties depending on which configuration
property changed.
- Parameters:
- {pwlib.appEvent.configChange} ev
- The application event object.
- See:
- PaintWeb.config.configFile The configuration file.
- pwlib.xhrLoad The library function being used for creating the XMLHttpRequest object.
onreadystatechange
event.
- Parameters:
- {XMLHttpRequest} xhr
- The XMLHttpRequest object being handled.
- See:
- PaintWeb#configLoad The method which issues the XMLHttpRequest request for loading the configuration file.
The scripts and styles loaded are not removed, since they might be used by other PaintWeb instances.
The pwlib.appEvent.appDestroy application event is dispatched before the current instance is destroyed.
This method determines the mouse position relative to the canvas
element, after which it invokes the method of the currently active tool
with the same name as the current event type. For example, for the
mousedown
event the tool.mousedown()
method is invoked.
The mouse coordinates are stored in the PaintWeb#mouse object. These properties take into account the current zoom level and the image scroll.
- Parameters:
- {Event} ev
- The DOM Event object.
- Returns:
- {Boolean} True if the tool event handler executed, or false otherwise.
This method determines the key the user pressed, based on the ev DOM Event object, taking into consideration any browser differences. Two new properties are added to the ev object:
- ev.kid_ is a string holding the key and the modifiers list (Control, Alt and/or Shift). For example, if the user would press the key A while holding down Control, then ev.kid_ would be "Control A". If the user would press "9" while holding down Shift, then ev.kid_ would be "Shift 9".
- ev.kobj_ holds a reference to the keyboard shortcut definition object from the configuration. This is useful for reuse, for passing parameters from the keyboard shortcut configuration object to the event handler.
In PaintWeb.config.keys one can setup the keyboard shortcuts. If the keyboard combination is found in that list, then the associated tool is activated.
Note: this method includes some work-around for making the image zoom keys work well both in Opera and Firefox.
- Parameters:
- {Event} ev
- The DOM Event object.
- See:
- PaintWeb.config.keys The keyboard shortcuts configuration.
- pwlib.dom.KeyboardEventListener The class dealing with the cross-browser differences in the DOM keyboard events.
keypress
event handler for inputs of type=number.
This function only handles cases when the key is Up or
Down. For the Up key the input value is increased,
and for the Down the value is decreased.
- Parameters:
- {Event} ev
- The DOM Event object.
- See:
- PaintWeb#ev_keyboard
load
event handler for each extension script.
If the extension object being constructed has the
extensionRegister()
method, then it will be invoked, allowing
any custom extension registration code to run. If the method returns false,
then the extension will not be registered.
Once the extension is successfully registered, this method dispatches the pwlib.appEvent.extensionRegister application event.
- Parameters:
- {String} id
- The ID of the new extension. The extension object constructor must exist in pwlib.extensions.
- Returns:
- {Boolean} True if the extension was successfully registered, or false if not.
- See:
- PaintWeb#extensionUnregister allows you to unregister extensions.
- PaintWeb#extensions Holds all the instances of registered extensions.
- pwlib.extensions Holds all the extension classes.
If the extension object being destructed has the
extensionUnregister()
method, then it will be invoked,
allowing any custom extension removal code to run.
Before the extension is unregistered, this method dispatches the pwlib.appEvent.extensionUnregister application event.
- Parameters:
- {String} id
- The ID of the extension object you want to unregister.
- Returns:
- {Boolean} True if the extension was removed, or false if it does not exist or some error occurred.
- See:
- PaintWeb#extensionRegister allows you to register new extensions.
- PaintWeb#extensions Holds all the instances of registered extensions.
- pwlib.extensions Holds all the extension classes.
- See:
- PaintWeb.config.guiStyle The interface style file.
- PaintWeb.config.guiScript The interface script file.
- pwlib.gui The interface object.
onreadystatechange
event.
- Parameters:
- {XMLHttpRequest} xhr
- The XMLHttpRequest object being handled.
- See:
- PaintWeb#guiScriptReady The method which issues the XMLHttpRequest request for loading the interface markup file.
load
event handler for the PaintWeb GUI script. This
method creates an instance of the GUI object that just loaded and starts
loading the GUI markup.
- See:
- PaintWeb.config.guiScript The interface script file.
- PaintWeb.config.guiMarkup The interface markup file.
- pwlib.gui The interface object.
- pwlib.xhrLoad The library function being used for creating the XMLHttpRequest object.
Once the history state has been updated, this method dispatches the pwlib.appEvent.historyUpdate event.
- Returns:
- {Boolean} True if the operation was successful, or false if not.
Once the history state has been updated, this method dispatches the pwlib.appEvent.historyUpdate event.
- Parameters:
- {Number|String} pos
- The history position to jump to.
If the value is a number, then it must point to an existing index in the PaintWeb#history.states array.
If the value is a string, it must be "undo" or "redo".
- Returns:
- {Boolean} True if the operation was successful, or false if not.
- Returns:
- {Boolean} True if the operation was successful, or false if not.
- See:
- PaintWeb#historyGoto The method invoked by this command.
This method dispatches the pwlib.appEvent.historyUpdate event.
- Returns:
- {Boolean} True if the operation was successful, or false if not.
- See:
- PaintWeb#historyGoto The method invoked by this command.
- Parameters:
- ev
The content of the image is retained only if the browser implements the
getImageData
and putImageData
methods.
This method dispatches three application events:
pwlib.appEvent.imageSizeChange, pwlib.appEvent.canvasSizeChange
and pwlib.appEvent.imageCrop. The imageCrop
event is
dispatched before the image is cropped. The imageSizeChange
and canvasSizeChange
events are dispatched after the image is
cropped.
- Parameters:
- {Number} cropX
- Image cropping start position on the x-axis.
- {Number} cropY
- Image cropping start position on the y-axis.
- {Number} cropWidth
- Image crop width.
- {Number} cropHeight
- Image crop height.
- Returns:
- {Boolean} True if the image was cropped successfully, or false if not.
This method dispatches two application events: pwlib.appEvent.imageSizeChange and pwlib.appEvent.canvasSizeChange.
- Parameters:
- {Element} importImage
- The image element you want to load into the Canvas.
- Returns:
- {Boolean} True if the operation was successful, or false if not.
This method dispatches the pwlib.appEvent.imageSave event.
Note: the "Save image" operation relies on integration
extensions. A vanilla configuration of PaintWeb will simply open the the
image in a new tab using a data: URL. You must have some event listener for
the imageSave
event and you must prevent the default action.
If the default action for the imageSave
application event
is not prevented, then this method will also dispatch the
pwlib.appEvent.imageSaveResult application event.
Your event handler for the imageSave
event must dispatch
the imageSaveResult
event.
- Parameters:
- {String} type Optional, Default: "auto"
- Image MIME type. This tells the browser which
format to use when saving the image. If the image format type is not
supported, then the image is saved as PNG.
You can use the resulting data URL to check which is the actual image format.
When type is "auto" then PaintWeb checks the type of the image currently loaded (PaintWeb.config.imageLoad). If the format is recognized, then the same format is used to save the image.
- Returns:
- {Boolean} True if the operation was successful, or false if not.
imageSaveResult
application event handler. This method
PaintWeb-related stuff: for example, the PaintWeb.image.modified
flag is turned to false.
- Parameters:
- {pwlib.appEvent.imageSaveResult} ev
- The application event object.
- See:
- The method which allows you to save the image.
- Parameters:
- {mixed} ev
- An event object which might have the shiftKey property. If the property evaluates to true, then the zoom level will increase twice more than normal.
- Returns:
- {Boolean} True if the operation was successful, or false if not.
- See:
- PaintWeb#imageZoomTo The method used for changing the zoom level.
- PaintWeb.config.zoomStep The value used for increasing the zoom level.
- Parameters:
- {mixed} ev
- An event object which might have the shiftKey property. If the property evaluates to true, then the zoom level will decrease twice more than normal.
- Returns:
- {Boolean} True if the operation was successful, or false if not.
- See:
- PaintWeb#imageZoomTo The method used for changing the zoom level.
- PaintWeb.config.zoomStep The value used for decreasing the zoom level.
- Parameters:
- ev
- Returns:
- {Boolean} True if the operation was successful, or false if not.
- See:
- PaintWeb#imageZoomTo The method used for changing the zoom level.
This method dispatches the pwlib.appEvent.imageZoom application event before zooming the image. Once the image zoom is applied, the pwlib.appEvent.canvasSizeChange event is dispatched.
- Parameters:
- {Number|String} level
- The level you want to zoom the image to.
If the value is a number, it must be a floating point positive number, where 0.5 means 50%, 1 means 100% (normal) zoom, 4 means 400% and so on.
If the value is a string it must be "+" or "-". This means that the zoom level will increase/decrease using the configured PaintWeb.config.zoomStep.
- Returns:
- {Boolean} True if the image zoom level changed successfully, or false if not.
This method is asynchronous, meaning that it will return much sooner before the application initialization is completed.
- Parameters:
- {Function} handler Optional
- The
appInit
event handler. Your event handler will be invoked automatically when PaintWeb completes loading, or when an error occurs.
- Returns:
- {Boolean} True if the initialization has been started successfully, or false if not.
The layer Canvas element will have the background rendered with the color from PaintWeb.config.backgroundColor.
If PaintWeb.config.imageLoad is defined, then the image element is inserted into the Canvas image.
All the Canvas event listeners are also attached to the buffer Canvas element.
- Returns:
- {Boolean} True if the initialization was successful, or false if not.
- See:
- PaintWeb#ev_canvas The global Canvas events handler.
- Returns:
- {Boolean} True if the initialization was successful, or false if not.
This method dispatches the pwlib.appEvent.appInit event.
Shadows support is also determined. The PaintWeb#shadowSupported value is updated accordingly.
- Returns:
- {Boolean} True if the initialization was successful, or false if not.
This method dispatches the pwlib.appEvent.appInit event.
- Parameters:
- {String} msg
- The error message.
load
event handler for the JSON library script.
- See:
- PaintWeb.config.lang The language you want for the PaintWeb user interface.
- pwlib.xhrLoad The library function being used for creating the XMLHttpRequest object.
onreadystatechange
event.
- Parameters:
- {XMLHttpRequest} xhr
- The XMLHttpRequest object being handled.
- See:
- PaintWeb#langLoad The method which issues the XMLHttpRequest request for loading the language file.
- Returns:
- {Boolean} True if the operation was successful, or false if not.
load
event handler for the PaintWeb library script.
- Parameters:
- {String} url
- The script URL you want to insert.
- {Function} handler Optional
- The
load
event handler you want.
- Parameters:
- {Event} ev Optional
- The DOM Event object which generated the request.
- Returns:
- {Boolean} True if the operation was successful, or false if not.
- See:
- The command implementation.
- Parameters:
- {Event} ev Optional
- The DOM Event object which generated the request.
- Returns:
- {Boolean} True if the operation was successful, or false if not.
- See:
- The command implementation.
- Parameters:
- {Event} ev Optional
- The DOM Event object which generated the request.
- Returns:
- {Boolean} True if the operation was successful, or false if not.
- See:
- The command implementation.
The pwlib.appEvent.shadowAllow event is dispatched.
The pwlib.appEvent.shadowAllow event is dispatched.
- Parameters:
- {CanvasRenderingContext2D} context
- The 2D context where you want to restore the state.
- {Object} stateObj
- The state object saved by the PaintWeb#stateSave method.
- Returns:
- {Boolean} True if the operation was successful, or false if not.
- Parameters:
- {CanvasRenderingContext2D} context
- The 2D context of the Canvas element you want to save the state.
- Returns:
- {Object} The object has all the state properties and values.
- Parameters:
- {String} id
- The stylesheet ID. This is used to avoid inserting the same style in the document.
- {String} url
- The URL of the stylesheet you want to insert.
- {String} media Optional, Default: 'screen, projection'
- The media attribute.
- {Function} handler Optional
- The
load
event handler.
This method dispatches the pwlib.appEvent.configChange event twice for each color (strokeStyle and fillStyle).
The id provided must be of an existing drawing tool, one that has been installed.
The ev argument is an optional DOM Event object which is useful when dealing with different types of tool activation, either by keyboard or by mouse events. Tool-specific code can implement different functionality based on events.
This method dispatches the pwlib.appEvent.toolPreactivate event before creating the new tool instance. Once the new tool is successfully activated, the pwlib.appEvent.toolActivate event is also dispatched.
- Parameters:
- {String} id
- The ID of the drawing tool to be activated.
- {Event} ev Optional
- The DOM Event object.
- Returns:
- {Boolean} True if the tool has been activated, or false if not.
- See:
- PaintWeb#toolRegister Register a new drawing tool.
- PaintWeb#toolUnregister Unregister a drawing tool.
- pwlib.tools The object holding all the drawing tools.
- pwlib.appEvent.toolPreactivate
- pwlib.appEvent.toolActivate
load
event handler for each tool script.
This method dispatches the pwlib.appEvent.toolRegister application event.
- Parameters:
- {String} id
- The ID of the new tool. The tool object must exist in pwlib.tools.
- Returns:
- {Boolean} True if the tool was successfully registered, or false if not.
- See:
- PaintWeb#toolUnregister allows you to unregister tools.
- pwlib.tools Holds all the drawing tools.
- pwlib.appEvent.toolRegister
- Parameters:
- {Number} x
- The x-axis location.
- {Number} y
- The y-axis location.
This method dispatches the pwlib.appEvent.toolUnregister application event.
- Parameters:
- {String} id
- The ID of the tool you want to unregister.
- Returns:
- {Boolean} True if the tool was unregistered, or false if it does not exist or some error occurred.
- See:
- PaintWeb#toolRegister allows you to register new drawing tools.
- pwlib.tools Holds all the drawing tools.
- pwlib.appEvent.toolUnregister
The pwlib.appEvent.canvasSizeChange application event is dispatched.