Omnimaga: The Coders Of Tomorrow
Welcome, Guest. Please login or register.
 
Omnimaga: The Coders Of Tomorrow
20 June, 2013, 02:39:11 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   home   news downloads projects tutorials misc forums rules new posts irc about Login Register  
+-OmnomIRC

You must Register, be logged in and have at least 40 posts to use this shout-box! If it still doesn't show up afterward, it might be that OmnomIRC is disabled for your group or under maintenance.

Note: You can also use an IRC client like mIRC, X-Chat or Mibbit to connect to an EFnet server and #omnimaga.

Pages: [1]   Go Down
  Print  
Author Topic: onmousemove and onmousedown -  (Read 1094 times) Bookmark and Share
0 Members and 1 Guest are viewing this topic.
Munchor
LV13 Extreme Addict (Next: 9001)
*************
Offline Offline

Gender: Male
Last Login: 13 June, 2013, 19:29:09
Date Registered: 16 October, 2010, 15:39:13
Location: Position
Posts: 6209


Topic starter
Total Post Ratings: +174

View Profile
« on: 02 October, 2011, 21:15:42 »
0

I have this function, that's called by onmousemove="checkmouse(event)"; in body.


1
2
3
4
5
6
7
function checkmouse(event)
{
  var x = event.clientX - drawingCanvas.offsetLeft;
  var y = event.clientY - drawingCanvas.offsetTop;

  context.fillRect(x, y, 5, 5);
}

I was wondering how I can check if the mouse is both moving and down. I can do them separately (onmousemove or onmousedown), but not together. Do you have any ideas? Thanks.
Logged
Ikkerens
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Male
Last Login: 17 June, 2013, 16:07:15
Date Registered: 12 June, 2010, 13:23:23
Location: Below a falling block.
Posts: 379


Total Post Ratings: +19

View Profile WWW
« Reply #1 on: 02 October, 2011, 22:03:04 »
0

You could try defining both window.onmousedown and and then defining window.onmousemove as well.
Logged


Splut for Android [----------]
Paused/halted indefinitely, might be abandoned, our graphic designer quit and the rest of us simply doesn't have the time to work on it...
Munchor
LV13 Extreme Addict (Next: 9001)
*************
Offline Offline

Gender: Male
Last Login: 13 June, 2013, 19:29:09
Date Registered: 16 October, 2010, 15:39:13
Location: Position
Posts: 6209


Topic starter
Total Post Ratings: +174

View Profile
« Reply #2 on: 02 October, 2011, 22:06:52 »
0

You could try defining both window.onmousedown and and then defining window.onmousemove as well.


1
2
3
4
5
6
7
8
9
10
11
12
function checkmouse(event)
{
  window.onmousedown = mousedown;

  function mousedown()
  {
    var x = event.clientX - drawingCanvas.offsetLeft;
    var y = event.clientY - drawingCanvas.offsetTop;

    context.fillRect(x, y, 5, 5);
  }
}

I changed to this, it works, but there's an issue. I'm trying to make a mini drawing application, so I need it to work like MS Paint, when the mouse is down and moving it draws a line.

There's an issue though because it's not smooth this way, I have to keep releasing the mouse and pressing it again.
« Last Edit: 02 October, 2011, 22:15:36 by ephan » Logged
Deep Thought
So much to do, so much time, so little motivation
Administrator
LV13 Extreme Addict (Next: 9001)
*
Offline Offline

Gender: Male
Last Login: 17 June, 2013, 03:12:44
Date Registered: 19 May, 2009, 08:00:00
Location: The Universe
Posts: 7832


Total Post Ratings: +713

View Profile WWW
« Reply #3 on: 03 October, 2011, 00:38:46 »
+1

Define some variable to represent the state of the mouse as down or up, then make it "true" when mouseDown is triggered and "false" when mouseUp is triggered.
Logged




Munchor
LV13 Extreme Addict (Next: 9001)
*************
Offline Offline

Gender: Male
Last Login: 13 June, 2013, 19:29:09
Date Registered: 16 October, 2010, 15:39:13
Location: Position
Posts: 6209


Topic starter
Total Post Ratings: +174

View Profile
« Reply #4 on: 03 October, 2011, 08:59:13 »
0

Define some variable to represent the state of the mouse as down or up, then make it "true" when mouseDown is triggered and "false" when mouseUp is triggered.

Yes, but when do I check if it's up or down?
Logged
Ikkerens
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Male
Last Login: 17 June, 2013, 16:07:15
Date Registered: 12 June, 2010, 13:23:23
Location: Below a falling block.
Posts: 379


Total Post Ratings: +19

View Profile WWW
« Reply #5 on: 03 October, 2011, 11:38:06 »
0

Yes, but when do I check if it's up or down?

window.onmousemove
Logged


Splut for Android [----------]
Paused/halted indefinitely, might be abandoned, our graphic designer quit and the rest of us simply doesn't have the time to work on it...
Munchor
LV13 Extreme Addict (Next: 9001)
*************
Offline Offline

Gender: Male
Last Login: 13 June, 2013, 19:29:09
Date Registered: 16 October, 2010, 15:39:13
Location: Position
Posts: 6209


Topic starter
Total Post Ratings: +174

View Profile
« Reply #6 on: 03 October, 2011, 21:41:25 »
0

Yes, but when do I check if it's up or down?

window.onmousemove

You could try defining both window.onmousedown and and then defining window.onmousemove as well.


1
2
3
4
5
6
7
8
9
10
11
12
function checkmouse(event)
{
  window.onmousedown = mousedown;

  function mousedown()
  {
    var x = event.clientX - drawingCanvas.offsetLeft;
    var y = event.clientY - drawingCanvas.offsetTop;

    context.fillRect(x, y, 5, 5);
  }
}

I changed to this, it works, but there's an issue. I'm trying to make a mini drawing application, so I need it to work like MS Paint, when the mouse is down and moving it draws a line.

There's an issue though because it's not smooth this way, I have to keep releasing the mouse and pressing it again.
« Last Edit: 03 October, 2011, 21:41:44 by ephan » Logged
Ikkerens
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Male
Last Login: 17 June, 2013, 16:07:15
Date Registered: 12 June, 2010, 13:23:23
Location: Below a falling block.
Posts: 379


Total Post Ratings: +19

View Profile WWW
« Reply #7 on: 03 October, 2011, 22:40:43 »
0

Are you even returning false then?
When overriding an user event you need to return false to "cancel" the original effect, try that Wink
Logged


Splut for Android [----------]
Paused/halted indefinitely, might be abandoned, our graphic designer quit and the rest of us simply doesn't have the time to work on it...
Munchor
LV13 Extreme Addict (Next: 9001)
*************
Offline Offline

Gender: Male
Last Login: 13 June, 2013, 19:29:09
Date Registered: 16 October, 2010, 15:39:13
Location: Position
Posts: 6209


Topic starter
Total Post Ratings: +174

View Profile
« Reply #8 on: 03 October, 2011, 22:43:11 »
0


1
2
3
4
5
6
7
8
9
10
11
12
13
function checkmouse(event)
{
  window.onmousedown = mousedown;

  function mousedown()
  {
    var x = event.clientX - drawingCanvas.offsetLeft;
    var y = event.clientY - drawingCanvas.offsetTop;

    context.fillRect(x, y, 5, 5);
    return false;
  }
}

I tried that, I didn't know I had to return false; in order to cancel events when overriding them. However, this didn't work. Thanks!
Logged
Ikkerens
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Male
Last Login: 17 June, 2013, 16:07:15
Date Registered: 12 June, 2010, 13:23:23
Location: Below a falling block.
Posts: 379


Total Post Ratings: +19

View Profile WWW
« Reply #9 on: 03 October, 2011, 22:51:47 »
0

Are you returning false on onmousemove? Cause I don't see mousemove in your code
Edit: So I've been trying to create what you were making, and came with this:
(It's not using canvas like you, but it should give you the general idea)

Moved here: http://www.omnimaga.org/index.php?topic=10744
« Last Edit: 04 October, 2011, 13:55:44 by Ikkerens » Logged


Splut for Android [----------]
Paused/halted indefinitely, might be abandoned, our graphic designer quit and the rest of us simply doesn't have the time to work on it...
Munchor
LV13 Extreme Addict (Next: 9001)
*************
Offline Offline

Gender: Male
Last Login: 13 June, 2013, 19:29:09
Date Registered: 16 October, 2010, 15:39:13
Location: Position
Posts: 6209


Topic starter
Total Post Ratings: +174

View Profile
« Reply #10 on: 04 October, 2011, 15:22:49 »
0


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
var drawingCanvas = document.getElementById('mycanvas');

if (drawingCanvas.getContext)
{
  var context = drawingCanvas.getContext('2d');
}

function main()
{
 
}

function checkmouse(event)
{
  window.onmousedown = mousedown;
  window.onmousemove = mousemove;

  function mousemove()
  {
    return false;
  }

  function mousedown()
  {
    var x = event.clientX - drawingCanvas.offsetLeft;
    var y = event.clientY - drawingCanvas.offsetTop;

    context.fillRect(x, y, 5, 5);
  }
}

I am now returning false on mouse move, but it still doesn't work smoothly. I will check your link right away.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by EzPortal
Powered by MySQL Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Powered by PHP
Page created in 0.484 seconds with 31 queries.
Skin by DJ Omnimaga edited from SMF default theme with the help of tr1p1ea.
All programs, games and songs avaliable on this website are property of their respective owners.
Best viewed in Opera, Firefox, Chrome and Safari with a resolution of 1024x768 or above.