JavaScript
主页 > 网络编程 > JavaScript >

JavaScript实现鼠标移动事件画笔

2022-08-23 | 佚名 | 点击:

鼠标点击时可以在画板上画画
如果鼠标双击那么停止
移动进画板颜色改变移除时颜色改变

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

<!DOCTYPE html>

<html lang="en">

  <head>

    <meta charset="UTF-8" />

    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <meta http-equiv="X-UA-Compatible" content="ie=edge" />

    <title>Document</title>

  </head>

  <style>

    * {

      margin: 0;

      padding: 0;

      box-sizing: border-box;

    }

    .drawbox {

      width: 1100px;

      height: 600px;

      background-color: skyblue;

      position: relative;

    }

  </style>

  <body>

    <div class="drawbox"></div>

    <script>

      /* 

        1.鼠标点击时可以在画板上画画

        如果鼠标双击那么停止

        移动进画板颜色改变移除时颜色改变

        */

      var darwbox = document.querySelector("div");

      darwbox.onmouseenter = function() {

        darwbox.style.backgroundColor = "red";

      };

      darwbox.onmouseleave = function() {

        darwbox.style.backgroundColor = "skyblue";

      };

      var istrue = false;

      darwbox.onmousedown = function(e) {

        istrue = true;

      };

      darwbox.onmousemove = function(e) {

        if (istrue == true) {

          var x = e.pageX;

          var y = e.pageY;

          var circle = document.createElement("div");

          circle.style.width = "10px";

          circle.style.height = "10px";

          circle.style.backgroundColor = "#fff";

          circle.style.position = "absolute";

          circle.style.left = x - 5 + "px";

          circle.style.top = y - 5 + "px";

          circle.style.borderRadius = "50%";

          

          darwbox.appendChild(circle);

        }

      };

      darwbox.onmouseup = function(e) {

        istrue = false;

      };

    </script>

  </body>

</html>

效果图:

原文链接:https://blog.csdn.net/qq_43547255/article/details/121452529
相关文章
最新更新