-2

I'm working on a Flash project that has to be translated to HTML5 and SVG. In Flash there is something called a movieclip. A movieclip can be like a class inside a Flash movie.

So, if I wanted to make a swarm of butterflies...

  • I would create one butterfly class / MovieClip with variable definitions of class properties behaviour (such as flapping wings, random movement and maybe obstacle avoidance).

  • After that I just throw a lot of butterfly movieclips on the screen and watch them do their thing.

For example: Using a For-loop I could spawn multiple instances of the class like so...

for (int i = 0; i < 30; i++)
{
    my_Butterfly = new ButterFly();

    my_Butterfly.graphics = SVG_referenced_HERE;
    my_Butterfly.flappingwings = true;
    my_Butterfly.flapspeed = 10;
    my_Butterfly.x = math.random(i * 40);
    my_Butterfly.y = math.random(i * 20);

}

My Question:

How can I create a class in JavaScript where the visual element is an SVG animation and that same visual element can be controlled by other class properties such as the .x and .y positions?


Now, with SVG this seems to be much more complicated because I don't know how make something that resembles a movieclip.

I would put a hand full of references to the butterfly SVG in an HTML document and then, somehow, animate them all at the same time with code that manages all the butterflies, but that is not very elegant compared to the movieclip mechanism. In other words: It's not suited to build complex animations

I have asked a similar question a while ago: Previous question

Any ideas?

VC.One
  • 14,790
  • 4
  • 25
  • 57
Bigman74066
  • 408
  • 4
  • 12
  • I am sorry: this site is not about Aesthetics or elegance. Show your code efforts and attempts then someone may help you. Best luck. – statosdotcom Feb 01 '19 at 02:04
  • The question is about a concept that is commonly used in Flash but doesn't seem to have a equivalent in HTML5. I cannot produce code if I don't know what concept te use in HTML5. – Bigman74066 Feb 01 '19 at 18:43
  • I really tried to explain this as clearly as I can... Please let me know what's missing and I will add it... – Bigman74066 Feb 01 '19 at 18:53
  • As I said Bigman, this site is not about abstract concepts. Try asking again showing code efforts you've already done in order to obtain the desired results. Then probably you will get luckier. Please, don't let me be misunderstood, I am just trying to help you. All my respects about your question and your work. There is none here showing disregard about your matters, we are just employing our time and patience trying to explain how things work on this site. Do you job, dive into code, then return here with code doubts. You said you don't know what concept to use. So study html5 then dive into – statosdotcom Feb 01 '19 at 19:02
  • So, basically you're saying: great question, wrong website (?) – Bigman74066 Feb 01 '19 at 19:24
  • Basically I am saying right the opposite: great site, wrong question. And I think the moderators agreed, so your question was "put on hold as unclear what you're asking". They sent to you https://stackoverflow.com/help/how-to-ask too, if you can be a smallman and accept help just for a while. – statosdotcom Feb 01 '19 at 19:59
  • By "wrong website" I mean "wrong place to ask a conceptual question like this" as you indicated. Nothing wrong with Stack overflow! – Bigman74066 Feb 02 '19 at 12:17
  • I did everything you suggested. My question contans all elements discussed in "how-to-ask". I got two relevant answers, yet you put my question on hold. I'm trying to do things right here and I'm putting in a lot of effort to clearify my question. So please either help me out or take the question off of [hold] – Bigman74066 Feb 02 '19 at 12:20
  • 1
    Is it not possible to do classes in Javascript? Flash stage is like Canvas so spawn butterflies as class (of SVG data) and draw to Canvas. Have Javascript control each unique instance (butterfly) of the class. – VC.One Feb 05 '19 at 16:05
  • Thank you VC.One for reformatting my original question! I'm currently on to something that innvolves with added – Bigman74066 Feb 07 '19 at 17:27

1 Answers1

2

You can define your animation as a resource in <defs> tags and then use <use> to place it wherever you want, like:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <defs>
        <path id="myAnimation" class="wavingPath" d="M0,0C0,-20,100,20,100,0" stroke="black">
            <animate attributeName="d" attributeType="XML" from="M0,0C0,-20,100,20,100,0" to="M0,0C0,20,100,-20,100,0" dur="1s" repeatCount="indefinite"/>
        </path>
    </defs>
    <use x="10" y="10" xlink:href="#myAnimation" />
    <use x="100" y="100" xlink:href="#myAnimation" />
</svg>

That is probably the ugliest image I've ever created but it shows the concept. Two copies of the same animation. You can use css animation instead. The resource you define can be a group object containing many paths you animate.

aptriangle
  • 1,395
  • 1
  • 9
  • 11
  • Thanks for this answer, it covers only part of what I'm trying to do... In Flash I can put both actionscript and graphics together in one entity. It's conceptually different from how things are normally done in HTML5. The movieclips behave like objects that "inherit" from the base-class movieclip so to speak... – Bigman74066 Feb 01 '19 at 18:47
  • It's a brilliant concept that is not easy to explain if you never worked with Flash... – Bigman74066 Feb 01 '19 at 18:51
  • Nice approach. Congrats. – statosdotcom Feb 01 '19 at 19:05
  • 1
    I think the closest that html comes to that concept is iframes. Javascipt, SVG and HTML can all be embedded in one html file and then loaded into iframes as one entity. The problem with that is synchronization. Unless the parent accesses the JavaScript of the documents in the iframes, they would all start at different times as the iframes loaded. It's also more difficult to handle sizing of those iframes than it is in flash with clips. – aptriangle Feb 01 '19 at 21:35
  • Now there's something I can work with. Thanks for the idea! – Bigman74066 Feb 02 '19 at 10:51