首页前端开发HTMLhtml5 canvas fillRect坐标和大小的问题解决方法

html5 canvas fillRect坐标和大小的问题解决方法

时间2024-01-24 10:33:09发布访客分类HTML浏览730
导读:收集整理的这篇文章主要介绍了html5 canvas fillRect坐标和大小的问题解决方法,觉得挺不错的,现在分享给大家,也给大家做个参考。 fillRect(100,100,100,100 前2个100是指坐标,后2个10...
收集整理的这篇文章主要介绍了html5 canvas fillRect坐标和大小的问题解决方法,觉得挺不错的,现在分享给大家,也给大家做个参考。 fillRect(100,100,100,100) 前2个100是指坐标,后2个100是指宽和高。 @H_777_0@
今天学习htML5 的canvas,发现fillRect的坐标和大小一直不对,研究了半天,发现canvas的宽度和高度必须内联在canvas标签中才对。郁闷了半天。

错误的方式1:

复制代码代码如下:@H_512_14@
!doctyPE html>
html lang="en">
head>
meta charset="UTF-8">
tITle> Document/title>
style>
#mycanvas{
width: 200px;
height: 200px;
background: yellow;
}
/style>
/head>
body>
canvas id='mycanvas' > /canvas>
script>
VAR c = document.getElementById('mycanvas');
var ctx = c.getContext("2d");
ctx.fillStyle='#f36';
ctx.fillRect(100, 100, 100, 100);
/script>
/body>
/html>

错误的方式2:

复制代码代码如下:
!doctype html>
html lang="en">
head>
meta charset="UTF-8">
title> Document/title>
/head>
body>
canvas id='mycanvas' style='width:200px; height:200px; background:yellow'> /canvas>
script>
var c = document.getElementById('mycanvas');
var ctx = c.getContext("2d");
ctx.fillStyle='#f36';
ctx.fillRect(100, 100, 100, 100);
/script>
/body>
/html>

显示结果:

正确的方式:

复制代码代码如下:
!doctype html>
html lang="en">
head>
meta charset="UTF-8">
title> Document/title>
/head>
body>
canvas id='mycanvas' width='200px' height='200px' style='background:yellow'> /canvas>
script>
var c = document.getElementById('mycanvas');
var ctx = c.getContext("2d");
ctx.fillStyle='#f36';
ctx.fillRect(100, 100, 100, 100);
/script>
/body>
/html>

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

canvashtml5

若转载请注明出处: html5 canvas fillRect坐标和大小的问题解决方法
本文地址: https://pptw.com/jishu/585307.html
HTMl5的存储方式sessionStorage和localStorage详解 html5适合移动应用开发的12大特性

游客 回复需填写必要信息