-_-#【Canvas】measureText, translate, drawImage

时间:2022-10-25 13:17:43

-_-#【Canvas】measureText, translate, drawImage

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<canvas id="canvas" width="600" height="600" style="background:gray"></canvas>
<script>
var canvas = document.getElementById('canvas')
context = canvas.getContext('2d')
var text = 'Copyright'
var FONT_HEIGHT = 100 context.save()
context.font = FONT_HEIGHT + 'px Arial'
var textMetrics = context.measureText(text)
context.fillStyle = '#0000FF'
context.textBaseline = 'middle'
context.translate(canvas.width / 2, canvas.height / 2)
// translate(x,y) 平移,将画布的坐标原点向左右方向移动x,向上下方向移动y
context.fillRect(30, 30, 50, 50)
context.fillText(text, -textMetrics.width / 2, 0)
context.restore() context.fillRect(30, 30, 50, 50) context.drawImage(canvas, 0, 0, canvas.width, canvas.height, 0, 0, canvas.width * 2, canvas.height *2)
</script>
</body>
</html>

随机推荐

  1. 第二次冲刺-Runner站立会议01

    今天做了什么:主要看了gridview的使用方法 遇到的困难:与适配器的链接不会 明天准备做什么:尽量将gridview与baseadapter适配器连接起来

  2. 2、android Service 详细用法

    定义一个服务 在项目中定义一个服务,新建一个ServiceTest项目,然后在这个项目中新增一个名为MyService的类,并让它继承自Service,完成后的代码如下所示: ? 1 2 3 4 5 ...

  3. javascript继承&lpar;四&rpar;—prototype属性介绍

    js里每一个function都有一个prototype属性,而每一个实例都有constructor属性,并且每一个function的prototype都有一个constructor属性,这个属性会指向 ...

  4. Spring中MultipartHttpServletRequest实现文件上传 生成缩略图

    转贴自:http://my.oschina.net/nyniuch/blog/185266 实现图片上传  用户必须能够上传图片,因此需要文件上传的功能.比较常见的文件上传组件有Commons Fil ...

  5. 【策略】UVa 278 - Chess

    Chess  Almost everyone knows the problem of putting eight queens on an  chessboard such that no Quee ...

  6. PHP文件类型检查类-比较全的

    在CSDN上淘来的一个文件类型的类,还不错,留下自己看! <? /** * 检证文件类型类 * * @author */ class FileTypeValidation { // 文件类型,不 ...

  7. 一张漫画说尽IT开发过程

  8. C&plus;&plus; CRTP singleton

    C++ CRTP 是个很有意思的东西,因为解释原理的文章很多,但是讲怎么用的就不是很多了. 今天就稍微写下CRTP(奇异递归模板模式)的一个有趣的用法:Singleton(单例模式) 单例有很多中写法 ...

  9. asynicio模块以及爬虫应用asynicio模块&lpar;高性能爬虫&rpar;

    一.背景知识 爬虫的本质就是一个socket客户端与服务端的通信过程,如果我们有多个url待爬取,只用一个线程且采用串行的方式执行,那只能等待爬取一个结束后才能继续下一个,效率会非常低. 需要强调的是 ...

  10. Netty socket&period;io 启用Epoll 模式异常

    Epoll 环境为Linux 内核2.6 以上版本  Windows下不能启动 1:判断Linux环境 public static boolean isOSLinux() { Properties p ...