canvas

三天学会HTML5——SVG和Canvas的使用

不想你离开。 提交于 2020-01-07 16:03:23
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 在第一天学习了HTML5的一些非常重要的基本知识,今天将进行更深层学习 首先来回顾第一天学习的内容,第一天学习了新标签,新控件,验证功能,应用缓存等内容。 第2天将学习如何使用Canvas 和使用SVG 实现功能 Lab1—— 使用Canvas Canvas 是指定了长度和宽度的矩形画布,我们将使用新的HTML5 JavaScript,可使用HTML5 JS API 来画出各种图形。 初始化 1. 创建HTML页面 <html><head></head><body></body></html> 2. 在Body标签内添加Canvas <canvas id="MyCanvas" width="500px" height="500px" style="border:1px solid black;"> </canvas> 3. 在<head>标签内添加Script 标签 <head> <script type="text/javascript"> </script> </head> 4. 在Script 标签内创建JavaScript 函数 Draw ,Draw函数能够访问Canvas 对象 function Draw() { var ctx = document.getElementById('MyCanvas'

canvas的drawImage方法参数详解

房东的猫 提交于 2020-01-07 12:04:55
HTML5中引入新的元素canvas,其drawImage 方法允许在 canvas 中插入其他图像( img 和 canvas 元素) 。drawImage函数有三种函数原型: drawImage(image, dx, dy) 在画布指定位置绘制原图 drawImage(image, dx, dy, dw, dh) 在画布指定位置上按原图大小绘制指定大小的图 drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh) 剪切图像,并在画布上定位被剪切的部分: 参数 描述 img 规定要使用的图像、画布或视频 sx 可选。开始剪切图片的 x 坐标位置 sy 可选。开始剪切图片的 y 坐标位置 sw 可选。被剪切图像的宽度(就是裁剪之前的图片宽度,这里的宽度若小于图片的原宽。则图片多余部分被剪掉;若大于,则会以空白填充) sh 可选。被剪切图像的高度(就是裁剪之前的图片高度) dx 在画布上放置图像的 x 坐标位置 dy 在画布上放置图像的 y 坐标位置 dw 可选。要使用的图像的宽度(就是裁剪之后的图片高度,放大或者缩放) dh 可选。要使用的图像的高度(就是裁剪之后的图片高度,放大或者缩放) 第一个参数image可以用HTMLImageElement,HTMLCanvasElement或者HTMLVideoElement作为参数

draw square and rotate it?

こ雲淡風輕ζ 提交于 2020-01-07 09:50:13
问题 how come this doesn't work? does rotate only work with images? context.moveTo(60,60); context.lineTo(200,60); context.lineTo(200,200); context.lineTo(60,200); context.lineTo(60,60); context.stroke(); context.rotate(45 * Math.PI / 180); context.restore(); 回答1: You are rotating the whole canvas when you use context.rotate , and since the pivot point is defaulted at the coordinates (0, 0), your square sometimes will be drawn out of bounds. By moving the pivot point to the middle of the square,

Moving character on canvas but not on the screen

微笑、不失礼 提交于 2020-01-07 09:43:27
问题 I am making a 2d game in JavaScript but my character can only move around the length of the screen so I was wondering if there was a way to make the canvas move but my character stay on the middle of the screen could Simone please help me here is my code for the test game <html> <body> <canvas id="canvas"> </canvas> </body> </html> <script> var audio = new Audio('sounds/theServerRoom.mp3'); audio.play(); // Create the canvas var canvas = document.getElementById("canvas"); var context = canvas

Canvas实现雨滴效果

北城余情 提交于 2020-01-07 08:19:46
主要思路: 创建canvas元素; 注意: canvas并不是所有部分都能绘制图形,它像一个国画卷轴一样,可绘制部分只有宣纸部分。如果需要canvas画布局域填充整个cnavas宽高,需要进行设置。 canvas是行内元素。行内元素如果等于浏览器宽高的话,会使浏览器出现滚动条,因为行内元素有行高,设置为块级元素即可。 canvas有默认的宽高(300*150) 获取canvas元素,并设置canvas宽高等于整个浏览器窗口宽高; 获取浏览器窗口的大下(宽高) window.innerWidth; window.innerHeight; 当浏览器窗口宽高发生改变时,也需要重新设置canvas宽高; window.onresize = function(){};监听浏览器窗口的变化,在浏览器窗口变化的时候,执行获取浏览器窗口大小的方法,并给canvas进行重新赋值。 实现会动的图形。 向下播放多张静态的图片。一秒内要大于屏幕刷新的帧数(60) 也就是每隔1/60s执行一次函数 在每次绘制的正方形上添加一个背景色为白色蒙板。 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0">

HTML5 Canvas blending & IE / Edge

北城以北 提交于 2020-01-07 06:43:45
问题 I'm working on a project that makes use of Canvas blending - of course, IE/Edge still don't support blending modes. Some simplified code (fiddle here) that shows the issue: <canvas id="below" width="100" height="100"></canvas> <canvas id="texture" width="100" height="100"></canvas> <canvas id="composite" width="100" height="100" style="z-index:1x solid green;"></canvas> var can = document.getElementById('below'); var below = can.getContext('2d'); below.fillStyle = "#FF0000"; below.fillRect(0

HTML5 Canvas blending & IE / Edge

心已入冬 提交于 2020-01-07 06:43:13
问题 I'm working on a project that makes use of Canvas blending - of course, IE/Edge still don't support blending modes. Some simplified code (fiddle here) that shows the issue: <canvas id="below" width="100" height="100"></canvas> <canvas id="texture" width="100" height="100"></canvas> <canvas id="composite" width="100" height="100" style="z-index:1x solid green;"></canvas> var can = document.getElementById('below'); var below = can.getContext('2d'); below.fillStyle = "#FF0000"; below.fillRect(0

Knovajs / HTML5 Canvas - Rotation origin

被刻印的时光 ゝ 提交于 2020-01-07 05:51:12
问题 I have successfully created a button which rotates an image either clockwise or C-Clockwise. However, this button can only be used once. i.e. if i press CW i cannot then use CCW to revert the image back. Any ideas? $rw = $('#rotate_right'); $rw.on('click', function(event) { event.preventDefault ? event.preventDefault() : event.returnValue = false; darthVaderImg.offsetX(img_width / 2); darthVaderImg.offsetY(img_height / 2); // when we are setting {x,y} properties we are setting position of top

Knovajs / HTML5 Canvas - Rotation origin

自古美人都是妖i 提交于 2020-01-07 05:51:05
问题 I have successfully created a button which rotates an image either clockwise or C-Clockwise. However, this button can only be used once. i.e. if i press CW i cannot then use CCW to revert the image back. Any ideas? $rw = $('#rotate_right'); $rw.on('click', function(event) { event.preventDefault ? event.preventDefault() : event.returnValue = false; darthVaderImg.offsetX(img_width / 2); darthVaderImg.offsetY(img_height / 2); // when we are setting {x,y} properties we are setting position of top

not able to download final merged canvas

混江龙づ霸主 提交于 2020-01-07 05:23:49
问题 I am merging 2 canvas on 1. Everything works fine. I just want to download this 3rd canvas on a click. I am using below code for merging 2 canvas and than download the final canvas: function downloadCanvas() { var bottleCanvas = document.getElementById('canvas_thumb'); var designCanvas = document.getElementById('canvas'); var finCanvas = document.getElementById('finalcanvas'); var bottleContext = finCanvas.getContext('2d'); bottleContext.drawImage(bottleCanvas, 0, 0); bottleContext.drawImage