mouseIsPressed combined mouseMoved() not work - p5.js

I want to make a doodling application, drawing strokes when mouse is moving, in the mousemove event handler, below code works:
line(lastX, lastY, mouseX, mouseY);
lastX = mouseX;
lastY = mouseY;
However, when I wrap the codes into an if statement like:
if (mouseIsPressed) {
...
}
Nothing, if I move the codes into draw function and set that condition, it works. Why?
Thanks in advance.

From the mouseMoved() reference:
The mouseMoved() function is called every time the mouse moves and a mouse button is not pressed.
So the mouseMoved() function won't be called if you are pressing a mouse button. If you put some code into an if block like:
if (mouseIsPressed) {
...
}
and put that block into mouseMoved(), then that code will never be executed.
It's totally ok to put that if block directly in the draw() function. You don't have to use mouseMoved() in this case :)

Related

Can code be called outside of draw() in processing, other than in setup()

According to Processing's translation reference page reference page, "If translate() is called within draw(), the transformation is reset when the loop begins again." Is there any way to call code outside of draw(), other than setup()?
I'm new to Processing, please forgive me if I've overlooked something obvious.
Yes, there are a couple ways you can call translate() and other Processing functions outside of the draw() function.
You could use a static sketch that just draws a single frame without looping:
size(200, 200);
translate(width/2, height/2);
ellipse(0, 0, 100, 100);
This is a full program and will draw a circle in the center of the window.
Or you could put your drawing code inside an event function:
void setup(){
size(200, 200);
}
void draw(){
translate(0, 100);
}
void mousePressed(){
translate(100, 0);
background(32);
ellipse(0, 0, 100, 100);
}
This program draws a circle in the center of the window when the user presses the mouse. But note that the calls to translate() stack: the translate(0, 100) call in draw() and the translate(100, 0) call in mousePressed() are added together so the circle shows up at 100,100. The draw() function is called first, and then the event functions are called.
The reference is just telling you that translation is reset the next time draw() is called.
No, there cannot be called something outside the draw()-function. When you want to use translate() with variable values, then use variables, that define the translation. When you want to translate to the same point each time, then call translate() with constant values at the beginning of each loop...

Xamarin Forms: scroll position in scrollview

Is there no possibility to read and set the position of the scrollview? For example, I would like to start the app with the scroll position at the bottom, not at the top.
You can get the X and Y position of a ScrollView from the ScrollX and ScrollY properties of the ScrollView object. Then you can set the position with the ScrollView method ScrollToAsync.
var x = scrollView.ScrollX;
var y = scrollView.ScrollY;
...
bool animate = false;
scrollView.ScrollToAsync(x, y, animate);
I've a better approach to this!
You can add this to your onAppearing() method:
protected override void OnAppearing()
{
base.OnAppearing();
/*Other stuff*/
viewModel.Messages.CollectionChanged += (sender, e) =>
{
var target = viewModel.Messages[viewModel.Messages.Count - 1];
ItemsListView.ScrollTo(target, ScrollToPosition.End, false);
};
}
This means that if your collection change, you get the target which is the last item (the one in the bottom) the you use scrollTo target, position end, with animation false.
when you get to the page it will open in the bottom, you don't see the scrolling because animation is false!
For Android, I have found a solution:
I write a custom renderer and in the OnDraw override Method, I call FullScroll(FocusSearchDirection.Down) the first time when the method is called.
What it makes easy, the Android ScrollViewRenderer is subtype of Android.Widget.ScrollView.
At the moment, I don't need an implementation for iOS, so I have no solution for iOS.

KineticJS cancel drag instead of stop drag?

I have a kinetic stage and some objects that each are draggable. (See fiddle for example.)
My goal is to allow the user to always be able to middle click to pan the entire stage whether or not he middle clicks on empty space or on any shape drawn to the stage. I need to do this in a way that doesn't break the drag events of existing shapes. Meaning I need the shape dragStart and dragEnd events to be processed normally, but only on left click drag.
I'm able to detect the middle click event on the stage and call dragStop() on the shape if the drag was initiated on one, but calling dragStop() means that the dragstop event is fired and processing occurs on the shape.
// let's pretend that a mouse doesn't have more than 9 buttons
var mouseDown = [0, 0, 0, 0, 0, 0, 0, 0, 0];
var mouseDownCount = 0;
document.body.onmousedown = function(evt) {
++mouseDown[evt.button];
++mouseDownCount;
}
document.body.onmouseup = function(evt) {
--mouseDown[evt.button];
--mouseDownCount;
}
stage.on('dragstart', function(e){
if(mouseDownCount){
if(mouseDown[0]){
// Left mouse button drag
if (e.target.nodeType == 'Stage'){
// stop the drag for left click
e.target.stopDrag();
}
} else if (mouseDown[1]){
// Middle mouse button drag
if (e.target.nodeType != 'Stage'){
// stop the drag of the object for left click
e.target.listening(false);
e.target.stopDrag();
e.target.listening(true);
// start the stage drag
e.target.getStage().startDrag()
}
}
}
});
Is there a way to cancel the dragStart event after it has been fired, or in some other way tell dragEnd to not fire? I've tried setting e.target.listening(false) prior to calling dragStop, but that doesn't seem to be working.
Any ideas?
Try to use latest version from GitHub. It has functionality only with left mouse button.

Get mouse buttons state being in background

I need something like GetKeyboardState() but for mouse. I do not need to handle an event when the mouse button pressed, just need to know if the buttons are up or down at some moment. This is for toy animation program.
if (GetAsyncKeyState(VK_LBUTTON) < 0)
{
// left button is down
}
if (GetAsyncKeyState(VK_RBUTTON) < 0)
{
// right button is down
}

draw simple rectangle in MFC Dialog-Based

I wrote this code to draw a simple rectangle in a dialog , I also added ON_WM_PAINT() to my message map. but it didnt show anything on dialog to me ! I really appreciate it if anyone could tell my mistakes in code:
void Ctest4Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = 2;
int y = 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
//I want to draw a rectangle
dc.Rectangle(10,10,50,50);
}
else
{
CDialogEx::OnPaint();
}
}
Looks like your paint code only runs when the window is iconic? Why are you doing that?
Put it in the else block, after the call to CDialogEx::OnPaint().
Your first and biggest mistake is trying to draw directly in a dialog. While it is possible to do so, it's almost always a bad idea. A dialog should usually be treated as a container for controls.

Resources