apply kinectjs to regular canvas? - javascript

So I have a fairly well established paint app using HTML5 canvas! It has painting, erasing, undoing, etc. I would like to be able to add dynamic images, text, and shapes (drag and drop abilities as well as easy manipulation).
I just came across the KinectJS library, and it would be perfect!!! The only issue that I have is that I would rather not rewrite my entire application for KinectJS as this is for a container, not a canvas tag.
question:
Is there any way to add KineticJS layers/stages into a canvas tag so that they still can be made with Kinetic and have the different options, but my app will still function?
alternative:
I understand that this could be impossible...if this is so, can I apply my regular canvas functions to a Kinetic container so that the whole thing is in the kinetic container, but I do not need to rewrite the code.
note:
If both of the above are impossible, please explain why

From what I know the answer is no.
Kineticjs create the canvas DOM element within the container you give that is why most people use a div element as the container and that is why if you directly put a canvas it won't work. The thing is, it shouldn't change much of your code. You'll just have to call getCanvas().getElement() instead of your normal reference to the canvas and all your previous code will work.
Here is a jsfiddle of it.

Related

HTML Image map vs. multiple div elements

I am implementing an Angular component which has many little areas where user can click.
I see 2 ways how to do it:
use background image and define map areas where user can click, add click handler on the image.
use div for each clickable part and attach the click handler to parent element (so I don't have many click handlers on each div). Use the CSS to style each div so it looks like the image in 1.
The problem with 1. is that when the image changes I have to change the map coordinates too.
The advantage here is that it should work without problem in many browsers.
The advantage of the 2. is that I can style the component as I need (so it can be smaller, bigger, different font, etc.), but it can have performance impact when I need to show more than one such component.
Which way would you choose and why? Or are there any other possibilities?
Edit:
forgot to mention that the component should also work on mobile devices
I don't remember where but i just read a nice article about:
div(webkit-transform) vs svg vs canvas
To make it simple:
1-50 elements = divs & images
50-500 elements = svg
500 and more elements = canvas
this are here just to give you an idea ...then everything depends on every element and device.
Canvas would be the best solution for everything.
The problem on canvas is the click handler.You need to create a collision detection script.
(i used a canvas with a background image(worldmap),to show the dots in real time of the current users.).but it's not clickable... (there is a clickable legend under/over the map)
SVG is prbably the best solution in your case
like #mainguy said u can draw stuff and add eventHandlers (or one like on the parent element).and the performance is better than divs.
DIVS
Most of the time i use div sets with one eventhanlder. they are so easy to use and style.. but only squares or circles.. and if you start to style them you loose alot performance (box-shadow..).
If you don't style the divs you can use alot of them.Especially if you put the eventhandler on the parentNode.
that way you can handle 1000's of elements without problems.(but don't use position:absolute)
Image Map
Again ... if there are not too many elements this is prolly also a good solution.. the simplest ... (the simplest way to draw your simple shapes).As soon you have you static MAP values you can then transform your imagesize recalculation the map with the ratio.. so thats not a problem.
I would go for the image map if there are not to many elements.
else SVG.
Everything depends on how many elements you have.
is that when the image changes I have to change the map coordinates too
if you use divs you don't have to change coords?
** Mobile devices support more than ie browsers.
I would do this with a SVG library which are fast, easy to style and suporrted by most browsers.
D3.js
is available for Angular as directive. Have not worked with his one yet. But it seems to be very popular. Just look on their homepage for a WOW! effect.
Raphaƫl JS
Was used by me in many projects. It has the big advantage that it supports even old IEs (sic!).
Just make a search for Angular SVG and/or JS and you will find a lot of solutions.
Not knowing the exact requirements, my default choice would be 2.
There are drawbacks, such us inability to easily map non-rectangular areas. The advantages however are huge, including easy maintenance, possible responsiveness, more robust styling possibilities and more.
The sole amount of divs shouldn't cause any problems and I wouldn't worry about it.
There are cases in which I would look for different implementation, though.

Animating "Painting" of an image onto the paper in RaphaelJS over a path

I am looking to animate an image onto a canvas via a circle 'brush' [Think Photoshop default brush] wherever the circle moves on a path the image is revealed.
The image would be a bitmap, not a vector file.
The brush will not have any additional detail beyond displaying the image its 'painting'
As additional clarification, the circle brush is moving automatically the user does not move the brush. It is just animated along a path.
I am really curious to see how this can be done with Raphael.
Thanks in advance!
If you want to achieve something like that, you better think of using Canvas. Since Raphael is a library oriented exclusively to SVG. You can use non-svg images, but you are going to face lots of limitations.
On the other hand, Canvas is pixel oriented, so what you are trying to do is going to be easier.
Raphael is great when it comes to SVG and user interaction, because the elements you create, are nodes inside the DOM, and you can easily bind them to events.
If you still want to achieve something like that with Raphael, you are going to need to start digging a bit into Raphael's methods. Is to difficult for someone to explain the whole process. You better start trying, and ask some question on more particular problems.
Apart from Raphael's documentation, I recommend this exaggeratedly colorful, but useful site which have some working examples.
Either way I think you should reconsider using Raphale over Canvas!
I hope that helps!!

Irregular anchors and shapes using JS in HTML/HTML5

Is it possible to have irregular shapes like anchors on some surface in JS that can be clickable and it becomes a ball on runtime on page loads? I want to animate that surface that can be a ball and also want to track that which anchor shape is at particular point.
So is it possible using JS? How can this be achieved? I was unable to find any solution. Is there some JS library that can do this? Something like processing.js or Raphael js?
Could I suggest an HTML image map? You can embed an IMG tag in the page set it's usemap attribute and then define shapes on that image to be clickable areas.
It doesn't have as many features as a canvas but maybe a regular IMG element will do in this case.
I suggest using SVG with excelent Raphael js library. You can make interactive animations with pure javascript (i.e. without additional plugins) and those objects can be made irregular anchors (overriding click, mouseover and mouseout events to emulate anchor behaviour).
Another idea is to use HTML5 canvas, but this requires more code (the technology is still immature) and may fail to work under different browsers.

How would one create a triangle container for an image (x-browser)

How would I create a DIV containing an IMG where the DIV cuts the image into a triangle, thereby displaying only part of the image though a triangle.
so..
<div>
<img src='some_image' />
</div>
Where the image is a square, but the DIV containing the image is a triangle.
http://www.script-tutorials.com/creating-kaleidoscope-using-jquery-and-css/ solves this very well except this solution is not x-browser friendly (non-ie).
http://css3pie.com/ looks interesting, however this relies on PHP.
You can't create a non-rectangular DOM element.
There are a few ways to hack it.
Firstly, there is a method of using CSS borders with varying widths on each side of the element to make it look triangular. It will still be a rectangle, but it will look like a triangle.
There's a tutorial on this here: http://www.russellheimlich.com/blog/pure-css-shapes-triangles-delicious-logo-and-hearts/
The down-side of this approach is that it is limited to creating right-angled triangles. You can join several of them together to get around this, but then you don't have a single container for your image.
An alternative hacky way of doing it would be to place rotated elements on top of the main element so that they cover the appropriate parts of the image and make it look triangular. This works in all browsers, although IE does have some very nasty syntax to do rotation, and it's quite heavy on the browser, considering that you'd only be using it to make shapes.
Another option might be to use CSS transforms. However this is only supported by a minority of up-to-date browsers, so it won't work for most users.
A better approach might be to use a proper graphics library for this, rather than trying to shoe-horn it into a <div> element.
I'd recommend using Raphael. It's a Javascript library which can draw directly into the browser using SVG (or VML for IE). It's trivial to create triangles using it and to fill them with a graphic. See the examples on the Raphael home page to get you started.
Depending on what you want the outcome to be, as far as i'm aware you cant make a triangle DIV without Transform:; However one solution would be having a div positioned inside the div in question with a PNG cutting of half the image showing only the transparent part through. Not sure if this is a viable option for you though.

Div positioning on a canvas with arrows bindings

I'm searching for a little piece of javascript that can attach to a div (in data-x data-y attributes for example) the x,y coordinates of a draggable div. And then be able to draw arrows between them (using a data-targetid attribute?).
If the solution can look like a simple version of graphview, jsPlumb or vizster but without the complicated library, and only in javascript/html
There is a jQuery version of jsPlumb, looks like it's 14kb. What you're trying to do isn't default functionality, you're going to need a (somewhat complicated) library to accomplish it. If you're concerned with code bloat I'd start with the source code of jsPlumb and start pulling out what you don't need.

Categories