How to make temporary un-hit-testable paths in paper.js? - paperjs

I need to make temporary paths (which will get removed once some operation is completed) using paper.js but I do not want them to be available in the HitResult.
I can achieve this by adding a flag to such paths, have a wrapper function over HitTest api which removes such paths from the result.
But I do not think this is a good design. HitTest calculations can be sped up if such paths are ignored at initial stages.
Is there any other option to achieve this?

You can set the item as locked to skip it in the hitTest calculations.
var path = new Path.Circle(new Point(100, 70), 50);
path.fillColor = 'black';
path.locked = true;
function onMouseDown(e) {
console.log(e.item);
}
And a Sketch here
Quoting this thread:
For now it only affects Item#hitTest(). Any item that is locked is skipped there.

Related

Best way to force textures upload to GPU at scene load time?

I was wondering the best way to force textures upload to GPU at scene load? I’ve read GPU Texture Preloading section within Best Practices, but I’m not quite sure if that’s something that needs to be done texture by texture and element by element.
There's an old thread talking about it here, but it doesn't seem to have a happy ending so far :(
Would it make sense to make a traverse from the sceneEl, before the scene has loaded, obtain every texture and call document.querySelector('a-scene').renderer.setTexture2D(eachTexture, 0)?
Thanks
setTexture2D no longer exists as of r103. You can use something like this instead
const forceTextureInitialization = function() {
const material = new THREE.MeshBasicMaterial();
const geometry = new THREE.PlaneBufferGeometry();
const scene = new THREE.Scene();
scene.add(new THREE.Mesh(geometry, material));
const camera = new THREE.Camera();
return function forceTextureInitialization(texture) {
material.map = texture;
renderer.render(scene, camera);
};
}();
Yes, that makes sense to do.
Just call sceneEl.renderer.setTexture2D(texture, i), passing in the three.js texture. And I think it's better to cycle i to a different value each call between 1 and 8.

Changing el of Ractive Object

In our project we are using Ractive together with Backbone.
Backbone.View has a "setElement" method, that basically sets the el property of a Backbone.View, thus allowing to attach the View to a different element of the DOM.
I was wondering if there is a similar functionality for a Ractive object.
Simply changing the el property of a Ractive object doesn't do the trick.
var oRactive = new Ractive(
{
"data": someData,
"el": someDomElement,
"template": someTemplate
});
// ... after doing some other stuff we'd like to set oRactive do a different el
// this.doesn't do the trick
oRactive.el = someOtherDomElement;
// this puts the renderedHTML in our DOM element but binding doesn't work
$(someOtherDomElement).html(oRactive.renderedHTML());
I'm not really surprised that the above doesn't work. Question is: Is there a way to make it work or is it generally impossible?
I am aware that I could just append oRactive.el to "someOtherDomElement" but that's not quite what I want.
This isn't something that Ractive currently supports, though it might in future. You could try doing the following:
frag = document.createDocumentFragment();
// move contents into the document fragment...
while ( oRactive.el.firstChild ) {
frag.appendChild( oRactive.el.firstChild );
}
// ...then into the DOM
someOtherDomElement.appendChild( frag );
// update oRactive.el so oRactive.find() still works
oRactive.el = someOtherDomElement;
Ractive stores references to individual nodes - in most cases it doesn't care about the actual shape of the DOM, and your situation shouldn't pose any obstacles (though I'd be interested to know if you run into any bugs doing this).

Resetting target values in a composite effect

We need to be able to handle a "playable" (play/pause/seek) effect in which the nature of the effect cannot be determined at compile time.
The problem we are running into is resetting the target(s) state after the effect has completed. If we manually drag the seek slider back to the beginning, everything works fine. However, if we set the playheadTime of the composite effect back to 0, the effected targets retain their original value until the playheadTime gets to the correct position to effect the target.
Here is a simplified (as much as I could) test case with view source enabled:
http://www.openbaseinteractive.com/_tmp/PlayableEffectTest/
The problem is demonstrated if you let it play to the end, and then hit the play button to start it over.
What is the best way to go about manually resetting the target values given that the exact nature of the effect is unknown?
Many thanks for your time!
edit
I forgot to mention we are using Flex 4.5 preview release.
Have you tried:
effect.reverse()
More info
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/effects/IEffect.html
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/effects/IEffect.html#reverse()
Well it's a little kludgy, but I was able to accomplish this by calling some internal methods on the effect to capture the start values, then assigned those values to the targets on a reset.
import mx.core.mx_internal;
use namespace mx_internal;
private var _propertyChangesArray:Array;
protected function captureStartValues(effect:Object):void
{
effect.captureStartValues();
_propertyChangesArray = effect.propertyChangesArray;
}
protected function reset(effect:Object):void
{
for each(var change:PropertyChanges in _propertyChangesArray)
{
var target:Object = change.target;
for(var p:String in change.start)
{
if(target.hasOwnProperty(p))
{
var startVal:* = change.start[p];
var endVal:* = target[p];
if(!isNaN(startVal) && startVal != endVal)
{
target[p] = startVal;
}
}
}
}
effect.playheadTime = 0;
}
I don't know if this is the best way to accomplish this, but it seems to be working so far. I am absolutely open to suggestions for a better method.
Cheers!

Adobe Flex ActionScript prevent model tainting

I have some values stores in my model. I need to create a copy of those values, make some changes, and then output those changes without affecting the model values.
var my_source:Array = model.something.source
var output:Array = new Array();
for each (var vo:my_vo in my_source) {
if (vo.id == 1) {
vo.name = 'Foo';
output.push(vo);
}
else if (vo.id == 21) {
vo.name = 'Bar';
output.push(vo);
}
}
return output;
So, this works fine, except that any changes that are made when looping through my_source also seems to affect model.something. Why do changes to the my_source array affect the model? How do I prevent this from happening?
I've mentioned how to do this in my blog, but short answer is use ObjectUtil.copy(). What you're trying to do isn't copying since Flash uses reference based objects, so you're only copying the reference to the other array. By using ObjectUtil.copy(), you're doing what's called a 'deep copy' which is actually recreates the object in a new memory location.
You are dealing with references to data, not copies of data. This is how ActionScript-3 (and many other languages) works.
When you create the my_source variable, you are creating a reference to model.something.source, which also includes all of the references to your model objects. Further, when you loop through the my_vo objects, you are also getting a reference to these objects. This means that if you make changes to the object in this loop, you are making changes to the objects in the model.
How do you fix this? Inside your loop, you will need to make a copy of your object. I don't know what my_vo looks like, but if you have any other objects in that object tree, they would be references as well, which would probably require a "deep copy" to achieve what you want.
The easiest way (but usually not the most efficient way) to achieve a "deep copy" is to serialize and de-serialze. One way to achieve this:
function deepCopy(source:Object):* {
var serializer:ByteArray = new ByteArray();
serializer.writeObject(source);
serializer.position = 0;
return serializer.readObject();
}
Then, in your loop, you can make your copy of the data:
for each(var vo:my_vo in my_source) {
var copy:my_vo = deepCopy(vo);
// act on copy instead of vo
}

FLEX: getting a folder size

I'm triying to get a folder size by doing:
var FolderFile:File = new File("file:///SomePath/Folder");
var FolderSize: FolderFile.size;
But this gives me a value of 0, how can I get the folder size? is there anyway to do this?
Tranks
No, there's no way to do it automagically. Getting the size of the directory is a complex and potentially painfully slow operation. There could be 10s of thousands of files in a directory, or a directory could be located on a (slow?) network, not to mention tape storage and similar scenarios.
The file systems themselves don't store directory size information, and the only way to know it is to calculate it file-by-file, there's no quick/easy shortcut. So, you will have to rely on the solution you posted above, and, yes, it is going to be slow.
I want to know the size of the folder (like 10mb). Sorry for the second line, I write it wrong, it's:
var Foldersize:Number = FolderFile.size;
I just made a new class wich executes this function:
public function GetFolderSize(Source:Array):Number
{
var TotalSizeInteger:Number = new Number();
for(var i:int = 0;i<Source.length;i++){
if(Source[i].isDirectory){
TotalSizeInteger += this.GetFoldersize(Source[i].getDirectoryListing());
}
else{
TotalSizeInteger += Source[i].size;
}
}
return TotalSizeInteger;
}
In "Source" you pass the FolderFile.getDirectoryListing(), something like this:
var CC:CustomClass = new CustomClass();
var FolderSize:Number = CustomClass.GetFolderSize(FolderFile.getDirectoryListing());
But this is a very slow method, is there a more quick and easy way to know the folder size?
Sorry for my grammar, i'm just learning english.
Thanks

Resources