Wednesday, December 30, 2020

Pixi JS sprite click and drag mouse issue

 I've started a new project with Pixi JS. I've used this tutorial (along with a lot of stack overflow)

https://github.com/kittykatattack/learningPixi

and it's been a great site to get a better understanding of HTML5 development with pixi js framework. However, I ran into an issue with clicking an dragging sprites. The example the Pixi JS site 

https://pixijs.io/examples/#/interaction/dragging.js

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const app = new PIXI.Application({ backgroundColor: 0x1099bb });
document.body.appendChild(app.view);

// create a texture from an image path
const texture = PIXI.Texture.from('examples/assets/bunny.png');

// Scale mode for pixelation
texture.baseTexture.scaleMode = PIXI.SCALE_MODES.NEAREST;

for (let i = 0; i < 10; i++) {
    createBunny(
        Math.floor(Math.random() * app.screen.width),
        Math.floor(Math.random() * app.screen.height),
    );
}

function createBunny(x, y) {
    // create our little bunny friend..
    const bunny = new PIXI.Sprite(texture);

    // enable the bunny to be interactive... this will allow it to respond to mouse and touch events
    bunny.interactive = true;

    // this button mode will mean the hand cursor appears when you roll over the bunny with your mouse
    bunny.buttonMode = true;

    // center the bunny's anchor point
    bunny.anchor.set(0.5);

    // make it a bit bigger, so it's easier to grab
    bunny.scale.set(3);

    // setup events for mouse + touch using
    // the pointer events
    bunny
        .on('pointerdown', onDragStart)
        .on('pointerup', onDragEnd)
        .on('pointerupoutside', onDragEnd)
        .on('pointermove', onDragMove);

    // For mouse-only events
    // .on('mousedown', onDragStart)
    // .on('mouseup', onDragEnd)
    // .on('mouseupoutside', onDragEnd)
    // .on('mousemove', onDragMove);

    // For touch-only events
    // .on('touchstart', onDragStart)
    // .on('touchend', onDragEnd)
    // .on('touchendoutside', onDragEnd)
    // .on('touchmove', onDragMove);

    // move the sprite to its designated position
    bunny.x = x;
    bunny.y = y;

    // add it to the stage
    app.stage.addChild(bunny);
}

function onDragStart(event) {
    // store a reference to the data
    // the reason for this is because of multitouch
    // we want to track the movement of this particular touch
    this.data = event.data;
    this.alpha = 0.5;
    this.dragging = true;
}

function onDragEnd() {
    this.alpha = 1;
    this.dragging = false;
    // set the interaction data to null
    this.data = null;
}

function onDragMove() {
    if (this.dragging) {
        const newPosition = this.data.getLocalPosition(this.parent);
        this.x = newPosition.x;
        this.y = newPosition.y;
    }
}

has bunny's that you can click on and drag. However, if you click on the lower right leg of the bunny and start to drag, the sprite jumps so that the center of the bunny is where the mouse is:

It seems trivial but when you are using larger sprites, it becomes borderline non-functional. Looking at the code you can see bunny.anchor.set(0.5) will move the sprite's anchor to the center instead of the top left. If you comment this out, you will see the the bunny jump more when you attempt to click and drag.

So how to fix this? So far I haven't been able to find a mouse location related to sprint you are clicking on. However, you can extract the mouse location for the app (screen area) and the sprite's location. From this, you can calculate an offset x and y coordinate when to initially mouseDown and drag. The offset helps update the anchor coordinate to prevent the jumping around of the sprite like so


Here is the updated code. Hope this helps someone!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const app = new PIXI.Application({ backgroundColor: 0x1099bb });
document.body.appendChild(app.view);

// create a texture from an image path
const texture = PIXI.Texture.from('examples/assets/bunny.png');

// Scale mode for pixelation
texture.baseTexture.scaleMode = PIXI.SCALE_MODES.NEAREST;

// Sprite drag offset
let spriteMouseLocationOffsetX = 0;
let spriteMouseLocationOffsetY = 0;


for (let i = 0; i < 10; i++) {
    createBunny(
        Math.floor(Math.random() * app.screen.width),
        Math.floor(Math.random() * app.screen.height),
    );
}

function createBunny(x, y) {
    // create our little bunny friend..
    const bunny = new PIXI.Sprite(texture);

    // enable the bunny to be interactive... this will allow it to respond to mouse and touch events
    bunny.interactive = true;

    // this button mode will mean the hand cursor appears when you roll over the bunny with your mouse
    bunny.buttonMode = true;

    // center the bunny's anchor point
    bunny.anchor.set(0.5);

    // make it a bit bigger, so it's easier to grab
    bunny.scale.set(3);

    // setup events for mouse + touch using
    // the pointer events
     bunny
        .on('pointerdown', onDragStart)
        .on('pointerup', onDragEnd)
        .on('pointerupoutside', onDragEnd)
        .on('pointermove', onDragMove);

    //For mouse-only events
    // .on('mousedown', onDragStart)
    // .on('mouseup', onDragEnd)
    // .on('mouseupoutside', onDragEnd)
    // .on('mousemove', onDragMove);

    // For touch-only events
    // .on('touchstart', onDragStart)
    // .on('touchend', onDragEnd)
    // .on('touchendoutside', onDragEnd)
    // .on('touchmove', onDragMove);

    // move the sprite to its designated position
    bunny.x = x;
    bunny.y = y;

    // add it to the stage
    app.stage.addChild(bunny);
}

function onDragStart(event) {
    // store a reference to the data
    // the reason for this is because of multitouch
    // we want to track the movement of this particular touch
    this.data = event.data;
    this.alpha = 0.5;
    this.dragging = true;
    // get the mouse coursor location within the window
    const appCursorLocation = this.data.getLocalPosition(this.parent);
    // calculate the offset with the app cursor location - sprite location
    spriteMouseLocationOffsetX = appCursorLocation.x - this.x 
    spriteMouseLocationOffsetY = appCursorLocation.y - this.y 
}

function onDragEnd() {
    this.alpha = 1;
    this.dragging = false;
    // set the interaction data to null
    this.data = null;
    spriteMouseLocationOffsetX = 0;
    spriteMouseLocationOffsetY = 0;
}

function onDragMove() {
    if (this.dragging) {
        const newPosition = this.data.getLocalPosition(this.parent);
        // adjust the sprite prosition using the offset
        this.x = newPosition.x - spriteMouseLocationOffsetX;
        this.y = newPosition.y - spriteMouseLocationOffsetY;
    }
}







Thursday, September 5, 2019

Back from my blogging hiatus

Hello, I'm back from my blogging hiatus. Lots of things have happened in the last 7 years. I've been more focused on software development using Java and Spring framework. I'm starting to put together some new blog entries around my development experiences and also my home development environment.
Currently I have 2 raspberry pie 3b machines that are used for my development environment at home. One is a nas and the other is a Jenkins server. I also started using an old laptop for gitlab due to the ram requirements. Working with raspberry pies have been a little difficult due to a lot of docker images are not built for arm and I had to do my best at re-creating the Dockerfile.
Anyways, I'm going to be adding more content soon.

Monday, July 22, 2013

Saving profile documents difference between Java and LotusScript/Formula

I ran across an issue that has been driving me nuts! I'm not sure if this is an error or by design. When I am working with profiledocuments in LotusScript and Formula languages, I can pull the profile document without giving a unique key parameter because it isn't required.

However, in Java, I can pull that same profile document, using "" as a unique key because it's required, read the correct values, and save it. However, when I save the profile document in Java, it doesn't update the profile document that the LotusScript and Formula languages. It seems to save a profile document separate from the LotusScript and Formula.

After I save the Java version of the profile document, I can only retrieve the Java version. When I open the profile document using LotusScript / Formula, it only retrieves the LotusScript / Formula version. Even after using getprofiledocumentcollection shows only one document, the one that will show depends on the language you are using.

However, when I start using a unique key, this problem goes away. ug. Anyone else had this issue or found a fix?
New job

I accepted a new job back in December 2012 that does not involve Lotus Notes/Domino. So unfortunately I won't be posting any more on Lotus Notes/Domino. In the next month I will try and finish up my draft posts from over the years and post them. I will still answer any questions anyone has.

Sunday, September 2, 2012

Windows file share error "the referenced account is currently locked out and may not be logged on to"

I see this error from time to time when connecting to a network share from a new Windows 2008 server to an old Windows server. However, I can usually connect using \\servername from the old server to the new server. After a few times of putting in my password to connect, the user account on the old Windows server becomes locked out. So then I get the error message "Account is currently locked out and may not be logged on to".

I also get the error "the specified network password is not correct". This error message led me to the correct answer. Since the issue happened on a Windows server 2008, I followed the procedure in the reference link below:

1. run secpol.msc
2 Navigate to Local Policies > Security options > Network security: LAN manager authentication. On my Windows 2008 server, it was blank
3. Next I selected NTLMV2 session secure if negotiated

No reboot needed, it started working after the change. After I was finished copying files, I change it to "NTVLM2 responses only" value.



reference:
http://serverfault.com/questions/91797/windows7-the-specified-network-password-is-not-correct-when-the-password-is

Wednesday, August 22, 2012

Lotus Connections / IBM HTTP Server (Apache) root directory redirect to application

When configuring Lotus Connections to use IBM HTTP server, it's always annoyed me that when typing in the URL minus an application, it takes me to an IBM HTTP server help screen. I've configured a redirect in Lotus Connections 2.5, however, I was unable to find my documentation. There are a few ways to accomplish this but I'm going to cover the one that works best for me. ( might not be the best solution for you ) So this post is how to configure a URL redirect / rewrite from the root / to an application ( /homepage ) in Lotus Connections using IBM HTTP server.

1. First, locate your httpd.conf file the IBM HTTP Server is using. to load the module for redirect / rewrite, locate the line that says 

#LoadModule rewrite_module modules/mod_rewrite.so

and remove the # to uncomment

LoadModule rewrite_module modules/mod_rewrite.so

2. Next, locate the line that says DocumentRoot


and place a # in front of the line to comment it out.


3. Next locate the VirtualHost *:443
FYI,  blogger.com won't let me post the correct syntax for the VirtualHost *:443, please see the image below for the correct syntax
VirtualHost *:443
Servername lc.ozzyblogger.com
SSLEnable
/VirtualHost


and add two lines

VirtualHost *:443
Servername lc.ozzyblogger.com
SSLEnable
RewriteEngine on
RewriteRule ^/$ /homepage [R,L]
/VirtualHost

This will load the lc.theozzyblogger.com/homepage application when you type lc.ozzyblogger.com/ in the url  btw lc.theozzyblogger.com is not a real website, just an example :)

Note:

^ is the beginning of the line 
$ is the end of the line 
. is any single character except new line 
* is zero or more of character

4. Last, create a virtualhost entry for all port 80 requests to redirect to SSL at the end of the file

FYI, blogger.com won't let me post the correct syntax for the VirtualHost *:80, please see the image below for the correct syntax
VirtualHost *:80
RewriteEngine on
RewriteRule ^/$ /homepage [R,L]
# handy for seeing what's going on when the web server tries to redirect
#RewriteLog "C:/rwlog.txt"
#RewriteLogLevel 1
# if the port's not 443 (ssl)...
RewriteCond %{SERVER_PORT} !^443$
#...redirect it to the same page but make it SSL
RewriteRule ^(.*) https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
/VirtualHost


That will take care of any requests that are typed as http://lc.theozzyblogger.com and redirect / rewrite as https://lc.theozzyblogger.com/homepage


Here are the web pages used as reference:


Configuring the IBM HTTP Server for SSL
http://publib.boulder.ibm.com/infocenter/ltscnnct/v2r0/index.jsp?topic=/com.ibm.lc_2.0_IC/t_configure_ihs.html 
Redirecting non-SSL (HTTP) requests to SSL (HTTPS) requests with IBM HTTP Server or Apache, and WebSphere Application Server
http://www-01.ibm.com/support/docview.wss?uid=swg21107738

Wednesday, August 1, 2012

Difference between LOG_SESSION and LOG_DISABLE_SESSION_INFO

I ran across an issue with log.nsf file growing quite large. To help reduce the size of the log file I started looking at how to remove the following session information:

Open session for Test User1/Domain (Release 8.5.3)
Closed session for Test User1/Domain Databases accessed:   1  Documents read:   1   Documents written:   0

from the log.nsf miscellaneous events. I found two notes.ini parameters, LOG_SESSIONS and LOG_DISABLE_SESSION_INFO. They both do the same thing, but what is the difference? After firing up a test environment, LOG_DISABLE_SESSION_INFO=1 disables open session / closed session events on the console, and therefore the miscellaneous events. This parameter also disables usage details in the log.nsf ( views named Usage / by Date, Usage / by User, Usage / by Database, Usage / by Size will be empty).

LOG_SESSIONS=0 disables the open session / closed session events on the console, and therefore the miscellaneous events. However, the usage details are recorded in the log.nsf ( views named Usage / by Date, Usage / by User, Usage / by Database, Usage / by Size will not be empty). Hope this helps someone out there :)