Unable to create accurate copy of a Kinetic.js stage / layer - clone

I'm trying to build in a change history, rather than a diff style history, I've opted to save the entire object.
This becomes problematic because each copy of the object updates along side the original object.
The Kinetic.Node.clone method seemed like the right thing for me, but it doesn't seem to do what I expect it to do.
Pseudocode:
var History = function(){
var h = this;
h.history = [];
h.pointer = -1;
h.save = function() {
h.history = h.history.slice(0,h.pointer);
h.history.push(im.Stage.createCopy());
h.movePointer(1);
};
h.movePointer = function(diff) {
h.pointer += diff;
(h.pointer < 0 && (h.pointer = 0));
(h.pointer >= h.history.length && (h.pointer = h.history.length-1));
return h.pointer;
};
h.render = function() {
im.Stage = h.history[h.pointer].createCopy();
im.Stage.draw();
};
h.undo = function() {
h.movePointer(-1);
h.render();
};
h.redo = function() {
h.movePointer(1);
h.render();
};
};
How can I create an accurate copy of the stage?

The best method to build up a history system is to store your layers/elements after each operation into an array as serialized values, using layer.toJSON() .
If you use images on layers and eventhandlers that you want to display/work after you restore anything from the history then you will have to reattache images and eventhandlers to the objects/layers/etc because toJSON() does not store images and eventhandlers as it would have been too large data stored. Build up your history like this:
first, try to use projeqht's answer on this question .
second you will have to reattach the images and eventhandlers. With a trick given by markE on this question you can easily handle it.

The best thing for an accurate representation is to do:
var layers = stage.getChildren();
to get the layers. Then do:
var layerChildren = new Array();
for(var i=0; i<layers.length; i++)
layerChildren[i] = layers.getChildren();
each time you want to save a state.
This will store all your layers and their children in an array. Fairly efficient and accurate.
Now you just have to save the list of layers and list of children somewhere and then you can move back and forth between states.

Related

AS3 Particle Explosion Crash

Hellooooo, hope y'all are doing great.
A while ago, I asked a question about how to do particle explosions in AS3 when I was coming from AS2. Luckily, I got help from Organis (thank you so much btw), but every time I try export the animation in SWF, it keeps crashing my file and I'm not sure why?
I should probably preface that what I'm doing is specifically for animation. I'm not trying to make a game, just a simple script where the movieclip I created can explode into different objects in its timeline...if that made any sense.
In case anyone needs the actual file itself, you can download it right here: https://sta.sh/018lqswjfmp2
Here is the AS2 version in case anyone needs it: https://sta.sh/02fzsqon3ohw
Here is the code given to me by Organis:
// Allows the script to interact with the Particle class.
import Particle;
// Number of particles.
var maxparticles:int = 200;
// I imagine you will need to access the particles somehow
// in order to manipulate them, you'd better put them into
// an Array to do so rather then address them by their names.
var Plist:Array = new Array;
// Counter, for "while" loop.
var i:int = 0;
// The loop.
while (i < maxparticles)
{
// Let's create a new particle.
// That's how it is done in AS3.
var P:Particle = new Particle;
// The unique name for the new particle. Whatever you want it for.
P.name = "particle" + i;
// Enlist the newly created particle.
Plist.push(P);
// At the moment, the P exists but is not yet attached to the display list
// (or to anything). It's a new concept, there wasn't such thing in AS2.
// Let's make it a part of the display list so that we can see it.
addChild(P);
i++;
}
And in case anyone needs it, here is the code I used for AS2:
maxparticles = 200; //number of particles
i = 0; //counter, for "while" loop
while(i < maxparticles){
newparticlename = "particle" + i; //creates a new name for a new particle instance
particle.duplicateMovieClip(newparticlename, i); //duplicates our particle mc
i++;
}

AMCharts - Dynamically add children to tree map

I am thoroughly enjoying AMChart's many features but I couldn't find any way to dynamically add some children to a treemap.
I am trying to load additional children on "hit" for each element
for (var i = 0; i < this.maxDepthLevel; i++) {
const series = this.chart.seriesTemplates.create(i);
series.columns.template.events.on("hit", async function(ev) {
const data = ev.target.dataItem.dataContext;
children = await api.getChildrenOf(data.id);
ev.target.dataItem.treeMapDataItem.children.values.push(...children);
});
}
^ this doesn't work and when doing this and then zooming out, I get
I even tried changing the underlying data and then calling
this.chart.invalidateRawData();
but to no avail.
Does anyone have any experience with adding such dynamic children to a tree map?
I cannot simply load everything upfront, there are far too many possible layers of depth unfortunately and the request will be too large!
Rather than directly pushing child items to children values you need to do it like this :
for(var index=0; index < children.length; index++){
var newChildDataItem = new am4charts.TreeMapDataItem();
newChildDataItem.value = children[index].value;
newChildDataItem.name = children[index].name;
newChildDataItem.color = ev.target.dataItem.dataContext.color;
ev.target.dataItem.dataContext.children.insert(newChildDataItem);
}

Exporting Makehuman.js Three.js using THREE.OBJExporter

I am using https://github.com/makehuman-js/makehuman-js
The example exports the mesh from the source. So I am trying to get it from the scene with where it is changed.
When I try to export my scene to an obj file it is empty:
var objscene = new THREE.OBJExporter().parse( self.scene );
var output = JSON.stringify( objscene, null, 2 );
saveAs (new Blob([output], {type : 'text/plain;charset=utf-8'} ), 'Avatar.obj');
I can count the objects in the scene. There are four.
var scene_size = app.scene.children.length;
var i = 0;
while(i < scene_size){
alert(app.scene.children[i])
i = i + 1;
}
However they have no names so I add a name to my main human object.
// HUMAN
this.human = new makehuman.Human(this.resources);
this.human.name = 'human';
So now I can retrieve the name of the object named human.
var scene_size = app.scene.children.length;
var i = 0;
while(i < scene_size){
var thisone = app.scene.children[i]
alert(thisone.name)
i = i + 1;
}
So, I can demonstrate the objects exist. I will assign names to the other objects later. What I cannot understand is why my export is empty. The file is 1kb in size and there is only "" in it when I open it in my editor.
Any insight would be appreciated. I've been pounding it for a week and I am at a loss... Thanks!
OBJExporter.parse() does not return a JSON object. So it makes no sense to use JSON.stringify() in this context. Have a look at the actual output in this example (you will see it's just a plain string).
In any event, I recommend to use GLTFExporter instead since glTF is the recommended format of three.js. You can use code snippets from the following example for your own project.
https://threejs.org/examples/#misc_exporter_gltf

How do I store and retrieve a variable from an array using the Firefox Add-on SDK?

I am attempting to develop a Firefox extension.
I have a simple need. At this time in testing, I simply wish to define an array. When the button is pushed, the Current Window goes to the first URL in the array. When the button is pushed again, it goes to the next URL in the array.
I am of the impression that I need to store the present sequence in the array using simple-storage (ss.storage) yet I cannot make it work. I thought perhaps I could store the sequence information using a Cookie but the Add-on SDK appears to be very stringent. Perhaps there is a far simpler method but I cannot seem to identify what that would be.
Current Status - when the button is pushed, the code opens three separate windows for each URL in the array. If you could please help me to modify this code so that opens one URL from the array each time the button is pushed - as described above.
require("widget").Widget({
id: "view-source-widget",
label: "View Source",
contentURL: "http://www.mozilla.org/favicon.ico",
onClick: function() {
var arr = new Array("one.com","two.com","three.com");
for(var i=0; i<arr.length; i++) {
var value = arr[i];
var ss = require("simple-storage");
ss.storage.myURL= value;
var windows = require("windows").browserWindows;
windows.open(ss.storage.myURL);
}
}
});
If I understand correctly what you are trying to do - you don't need persistent storage, a normal global variable will do. The global variable indicates the array index at which you are right now. Something like this:
var urls = ["one.com","two.com","three.com"];
var urlIndex = 0;
...
onClick: function() {
var windows = require("windows").browserWindows;
windows.open(urls[urlIndex]);
// Increase index for the next click
urlIndex++;
if (urlIndex >= urls.length)
urlIndex = 0;
}
...

How do I apply the same animation to multiple items in AS3?

I have about 100 different MCs that I need to apply the following animation to.
total_bananas = 5;
frameCount = 0;
for (i=1;i<=total_bananas;i++)
{
thisMC = _root["mc"+i];
thisMC.startY = thisMC._y;
thisMC.rand = Math.random();
}
this.onEnterFrame = function ()
{
frameCount++;
for (i=1;i<=total_bananas;i++)
{
thisMC = _root["mc"+i];
thisMC._y = thisMC.startY + Math.sin(thisMC.rand*100+frameCount/10)*5;
}
}
how would I go about applying this animation to each of them individually? I don't need to populate 5 of the same MCs. This script is simply the perfect animation visually, but not lean and bespoke like it should be. I just need to make a lot of objects (all unique) bob up and down like they are tied to balloons. Also I was thinking this might be depreciated and there might be a waayy better way to do this.

Resources