I'm new to using PanResponder in React Native. I want to make draggable component (only ups and down, so I use Y value only). In onPanResponderMove I can get live value of Y by getting gestureState.dy.
Next, I made helper function that called in onPanResponderRelease to reset the position of dragged component when the touch is off. Here is the code:
resetPosition() {
Animated.timing(
// Animate value over time
this.position, // The value to drive
{
toValue: { x: 0, y: 0 }, // Animate to final value of 1
duration: 350
},
).start();
}
My question is; how can I get the live Y value when the component from it last position to reset position?
Related
I'm currently using Phaser 3 to represent my server's state.
Every x amount of time, I am sent the server's game state, this is what the client looks like:
var t1 = Date.now();
var serverUpdateDelta = 0;
Client.socket.on('usersPool', usersPool => {
// usersPool is an object containing all the user data of sockets connected on the server. Looks something like this:
/*
usersPool = {
"user1234": { x: 0, y: 0, direction: "right", moving: true },
"testuser": { x: 200, y: 250, direction: "down", moving: false }
}
*/
// keeping count of milliseconds between updates (usually around 500m)
serverUpdateDelta = Date.now() - t1;
// for every user connected on the server...
for(id in usersPool) {
let data = usersPool[id]; // this is the user's data
if(/* the player exists as a sprite in the game...*/) {
// THIS IS WHERE THE MAGIC SHOULD HAPPEN
} else {
genSprite(player);
}
}
});
The player's data contains a movementQueue, which is just an array of coordinates the user has been at. It might look a little like this:
[
{ x: 0, y: 0, direction: 'down', moving: false },
{ x: 5, y: 0, direction: 'right', moving: true },
{ x: 6, y: 0, direction: 'right', moving: false }
]
This is calculated on the server, but each movementStack (item in the movementQueue`) is generated every 25 milliseconds or so on the server.
The job now is, when receiving this movementQueue, to interpolate the values and move the sprite accordingly...
Attempt 1
I first tried making a function which would interpolate once the update was received, like so:
// THIS IS WHERE THE MAGIC SHOULD HAPPEN
// user's state on the client is set to an interpolated version of the one on the server
player.movementQueue = buffer(data.movementQueue);
The buffer will simply generate an interpolated array based on the serverUpdateDelta and game.loop.actualFps
then, in the Game.update function I ran the following:
for(id in spawnedPlayers) {
// this will remove the first movementStack from the queue and returns in
movementStack = spawnedPlayers[id].movementQueue.shift();
// we then take this movementStack and update the user to that position (and play the walking animation)
spawnedPlayers[id].update(movementStack);
}
So every game loop, we would remove a stack from the queue and set the user to it.
This did NOT work. The game loop seemed to run Way more times than there were frames in the queue, making the player look like they were moving a small distance very slowly...*:
player.movementQueue = player.movementQueue.concat(buffer(data.movementQueue));
But then something weird happened, where the game loop could not keep up with the movementQueue and the player would move extremely slowly...
Attempt 2
I then tried using tweens which would be really easy to implement, simply run:
// THIS IS WHERE THE MAGIC SHOULD HAPPEN
_this.tweens.timeline({
targets: player.sprite,
tweens: data.movementQueue, // [{x, y}, {x, y}, {x, y}]
duration: serverDeltaTime/movementQueue.length, // duration between each tween in ms
});
This worked ALMOST perfectly, except for one small detail:
Before, we would run a method for the player on each movementStack: player.update(movementStack), this method would take the direction of the user and animate the sprite accordingly. Now we have no way of doing this...
SO
What methods or techniques could I use? What am I missing? What could I implement? I ask this because I'm stuck at this point.
Thank you in advance for the help.
I am using Qt 5.9.3 commercial version.
Scenario
I have logic to be executed in Qt QML part of the my code. On click of some random button. I want to move a QML rectangle to another position. The other position is a calculated (x, y) position based on current position of the rectangle.
Following is sort of what I want to do:
Code:
import QtQuick 2.9
import QtQuick.Window 2.2
Window {
visible: true
width: 800
height: 600
title: qsTr("Hello World")
property bool some_boolean_flag: false
Rectangle {
id: my_rect_1
color: "blue"
width: parent.width / 4
height: parent.height / 4
z:1
property real current_x
property real current_y
x: {
if (some_boolean_flag) {
current_x = current_x + current_x/10
return current_x
}
else {
var default_x = parent.width/6
current_x = default_x
return current_x
}
}
y: {
if (some_boolean_flag) {
current_y = current_y + current_y/10
return current_y
}
else {
var default_y = parent.height/6
current_y = default_y
return current_y
}
}
}
Rectangle {
id: my_rect_2
color: "yellow"
width: parent.width/5
height: parent.height/5
x: parent.width - parent.width/4
y: parent.height - parent.height/4
z:2
MouseArea {
anchors.fill: parent
onClicked: {
// swapping the boolean flag between true and false
some_boolean_flag = !some_boolean_flag
}
}
}
}
The objective of this question:
Note that I want to do the calculation declaratively. I can easily create a QML function and do this declaring some global properties which is of course easy but doing this calculation is not the point of the question. My objective is to learn how to to do this declaratively. How to apply a calculation to a QQuickItem::x based on the QQuickItem::x's current value itself..
If you run the example code in this question, you will see that my_rect_1 just keeps swapping between 2 positions back and forth which is not what the logic really intends. The intention of the logic is to keep incrementing my_rect_1::x and my_rect_1::y by a certain factor which would cause my_rect_1 to get moved rightwards and downwards as you keeping clicking on the other rectangle that is my_rect_2.
Other issue
is that I keep getting the following error at runtime when I click on my_rect_2.
qrc:/main.qml:12:5: QML Rectangle: Binding loop detected for property "x"
I understand QML is complaining about the circular dependency of the property x in my_rect_1. How can I avoid this and achieve a declarative way of transforming x and y?
Potential solution I found from my searches
Looks like Translate QML Type can be useful in the logic I want to implement but how?
How can I achieve this behaviour without writing a QML function which does this transformation separately outside my_rect_1?
Well, "declaratively" would simply involve bindings, which are in their essence pretty much functions, with the added benefit of automatic reevaluation upon changes in the values involved in the expressions.
Here is a trivial example that will show a red dot on screen, and when clicking on the window, the dot will move to a location that is derived by the dot's current location and the clicked location, putting it right in the center of the imaginary line between the two coordinates:
ApplicationWindow {
id: main
width: 640
height: 480
visible: true
property real xpos: 10
property real ypos: 10
Rectangle {
id: rect
x: xpos
y: ypos
width: 10
height: 10
radius: 5
color: "red"
}
MouseArea {
anchors.fill: parent
onClicked: {
xpos = (xpos + mouseX) * .5
ypos = (ypos + mouseY) * .5
console.log(ypos)
}
}
}
Note that the xpos and ypos properties are practically redundand, and the same thing can be achieved without that, but it would involve manually manipulating the dot coordinates, thus its position will not be declared in a purely "declarative way". This way it is bound to the property values, and will keep on following them regardless of what process controls the values.
As for this part of your code:
x: {
if (some_boolean_flag) {
current_x = current_x + current_x/10
return current_x
}
else {
var default_x = parent.width/6
current_x = default_x
return current_x
}
}
It is the binding expression that defines the property value. If you assign a value to a property manually, it breaks the existing binding. What you do is you assign a value to the property in the expression that is supposed to do that automatically - pure nonsense, which also explains why it doesn't work as expected.
The appropriate format would be:
x: {
if (some_boolean_flag) {
return current_x + current_x/10
}
else {
var default_x = parent.width/6
return default_x
}
}
Which can actually be distilled down to:
x: some_boolean_flag ? current_x + current_x/10 : parent.width/6
and so on... And as you see, using this format removes the binding loop warnings and now you have behavior that matches the code logic intent.
I'm trying to modify the behavior of a vertical C3.js Spine chart so that the mousewheel scrolls the range of the data instead of zooming. I created an event listener using Prototype to change the range of the chart when someone scrolls inside its parent div like so:
$("chartDiv").observe("mousewheel", function(e, charts) {
if (charts.get("splineChart") != null) {
var cht = charts.get("splineChart");
var range = cht.axis.range();
if (e.wheelDelta > 0) {
cht.axis.range({
min: {
y: range.min.y - 10
},
max: {
y: range.max.y - 10
},
});
}
else {
cht.axis.range({
min: {
y: range.min.y + 10
},
max: {
y: range.max.y + 10
},
});
}
}
}.bindAsEventListener(this, chartList));
When testing in Chrome, the event is handled corrently and I have access to the chart object that I want to work with, but the max and min objects returned by range() have undefined values for x, y, and y2.
I've read through the API listed on http://c3js.org/reference.html#api-axis-range and it says is that calling range() with no argument should return the current min and max values for each axis.
What am I doing wrong here? I need to get and update the current displayed range for data in the chart.
is it possible to create tweens sequence in KineticJS according to data saved in array without recursion?
This is my current solution including recursion:
function tweenFunc( image )
{
setGlobalCoordinates();
tween = new Kinetic.Tween({
node: image,
duration: 1,
x: x,
y: y,
rotation: rot,
onFinish: function(){
if ( counter++ < arr.length )
{
tweenFunc( image );
}
}
});
tween.play();
}
I would like to have solution which creates tweens before first animation and following tweens start from final position of predecessor.
This project includes animation of 4 images in the same time for many position sequence.
If I have an image that I apply a filter to, e.g. Lomo filter, is there way to make that the current Caman instance?
Meaning, if I then want to then play about with the brightness on the image that I applied the filter to, and use this.revert(); to reset the brightness, I want it to revert to the canvas with the filter on it that I just applied.
Is this possible?
I'm having a nightmare with trying to apply many effects, only one at once (except for preset filters), and carry the state through...
If i understand, you want to apply filter ("Lomo") as shown on their example page and then fiddle with some properties (like brightness) and then revert changes back to "Lomo" filter?
Why not just click on filter ("Lomo") again?
EDIT:
You should probably take a look at guide and implement your own method with default values like in filters.
u.Filter.register("lomo", function (D) {
if (D == null) {
D = true
}
this.brightness(15);
this.exposure(15);
this.curves("rgb", [0, 0], [200, 0], [155, 255], [255, 255]);
this.saturation(-20);
this.gamma(1.8);
if (D) {
this.vignette("50%", 60)
}
return this.brightness(5)
});
I dont think your requirement comes "out of the box".
If i understand you correctly , You want to apply a filter and play with other effects like brightness and contrast etc.,
I made some code which will work according to your need
Caman('#canvas-camanImage',"./../media/caman.png", function () {
this.revert(false);
for(var i = 0 ;i<selectedPresets.length;i++){
this[selectedPresets[i]]();
}
for(var key in effect){
this[key](effect[key].value);
}
this.render(function () {
});
in the above code i am storing all effects like brightness contrast in effect variable like effect = {
brightness : {
min : -100,
max: 100,
value : 0
},
contrast : {
min : -100,
max: 100,
value : 0
},
saturation : {
min : -100,
max: 100,
value : 0
}
};
and presets in an array
presets = [
{filter:'vintage',name : 'Vintage'},
{filter:'lomo',name:'Lomo'},
{filter: 'clarity', name:'Clarity'},
{filter:'sinCity', name:'Sin City'}
];
So every time you add any preset or change any effect value i am changing the values in variable and rendering canvas again
It is working very fine for me Let me know if your concern is something else