canvas文字怎么换行?canvas文字换行的方法介绍


当前第2页 返回上一页

接下来我们来看一看将canvas文字换行的代码:

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

<!DOCTYPE html>

<html>

<head>

  <meta charset="UTF-8">

  <title>canvas自动换行</title>

  <style type="text/css">

    * {

      margin: 0

    }

    #main {

      width: 400px;

      margin: 20px auto 0;

    }

    #canvas, #editableWarp, #editable, #hideText {

      width: 400px;

      height: 125px;

      padding: 0;

      border: 0;

      background: pink;

      color: blue;

      font-size: 14px;

      font-family: 'sans-serif';

      position: relative;

      z-index: 1

    }

    #hideText {

      z-index: 0;

      position: absolute;

       word-break: break-word;

      word-wrap: break-word;

    }

    p {

      line-height: 32px;

    }

  </style>

</head>

<body>

  <div id="main">

    <p>

      输入:

    </p>

    <div id="editableWarp">

      <div id="hideText"></div>

      <textarea id="editable" placeholder="请输入文字..."></textarea>

    </div>

    <p>

      canvas输出:

    </p>

    <canvas id="canvas" width="400" height="125">您的浏览器不支持canvas</canvas>

  </div>

  <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>

  <script type="text/javascript">

    var $canvas = $('#canvas'),

        $editable = $('#editable'),

        $hideText = $('#hideText'),

        ctx = $(canvas)[0].getContext("2d");

    $editable.keyup(function handleEdittable () {

      var txt = $editable.val(),

          html = convertText(txt);

      $hideText.html(html);

      drawText();

    });

    function convertText(txt) {

      var html = txt.replace(/(\S)/ig, '<span>$1</span>');

      html = html.replace(/\n|\r/ig, '<br>');

      html = html.replace(/\s/ig, ' ');

      return html;

    }

    function drawText () {

      ctx.clearRect(0, 0, $(canvas).width(), $(canvas).height());

      var fontSize = $hideText.css('fontSize');

      ctx.font = fontSize + ' sans-serif';

      ctx.textAlign = 'conter';

      ctx.textBaseline = "top";

      ctx.fillStyle = 'red';

      $.each($("#hideText span"), function (i, item) {

        var pos = $(item).position();

        var txt = $(item).text();

        ctx.fillText(txt, pos.left, pos.top);

      });

    }

  </script>

</body>

</html>

canvas换行效果如下:

2345截图20180926135451.png

本篇文章到这里就结束了,关于canvas元素更多的知识可以参考HTML5开发手册学习具体内容。

以上就是canvas文字怎么换行?canvas文字换行的方法介绍的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

如何使用canvas画出平滑的曲线?(代码)

html5+canvas调用手机拍照功能实现图片上传功能(图文详解下篇)

使用html5 canvas api中的clip()方法裁剪区域图像代码实例

图文详解如何用canvas画实心圆和空心圆

图文详解如何用html5 canvas画一条直线

canvas学习系列一:初识canvas

canvas的图片处理

js html5 canvas绘制图片的方法

html5 canvas实现文本对齐的代码总结

html5如何使用canvas画空心圆与实心圆

更多相关阅读请进入《canvas》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...