我的代码是:
var canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
ctx.canvas.width = 40vw;
ctx.canvas.height = 40vh;
Run Code Online (Sandbox Code Playgroud)
它不起作用。在 JavaScript 中设置画布尺寸时是否可以使用 vw 和 vh?如果是这样,如何?
当它们是内容时,我可以缩放图像以填充窗口,如下所示:(没有任何html5或div)
<style type="text/css">
img {
width:100%;
height:100%;
margin:0px;
}
</style>
Run Code Online (Sandbox Code Playgroud)
和
<img src="wacard.png" alt="original image" title="">
Run Code Online (Sandbox Code Playgroud)
但
这失败了("html,body"和"body")
<style type="text/css">
body {
width:100%;
height:100%;
margin:0px;
}
</style>
Run Code Online (Sandbox Code Playgroud)
和
<body
style="background-image: url(wacard.png);">
</body>
Run Code Online (Sandbox Code Playgroud)
从这里拾取拾取:调整HTML5画布的大小以适应窗口但据我所知,它必须有一些比我知道的更奇特的代码,继续,...或者我做错了...
如何才能获得背景图像?(理想情况下,没有javascript)
我想使用 HTML 制作简单的形状。但形状需要很大。画布全屏显示
示例:http : //jsfiddle.net/xLgg43s9/1/embedded/result/
代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
* { margin: 0; padding: 0;}
body, html { height:100%; }
#canvas {
position:absolute;
width:100%;
height:100%;
}
</style>
</head>
<body>
<canvas id="canvas">
</canvas>
<script>
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
ctx.fillStyle = "#000";
var w=canvas.width;
var h=canvas.height;
ctx.fillRect(0,0,w,h);
ctx.fillStyle="#fff";
ctx.beginPath();
var a=w/2;
var b=0;
ctx.arc(a,b,20,0,Math.PI,false);
ctx.closePath();
ctx.fill();
ctx.stroke();
ctx.beginPath();
ctx.fillStyle="red";
ctx.fillRect(0,0,10,100);
ctx.stroke();
ctx.save();
ctx.translate(240, 120); …Run Code Online (Sandbox Code Playgroud) 我有一个程序,用于在鼠标拖动时在画布上移动对象.但是,我希望画布适合屏幕.我不知道如何实现这一目标.如果我将画布设为"width:100%; height:100%;",则该对象超出范围.
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css"/> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js">
</script>
<style>
body{ background-color: ivory; }
canvas{border:1px solid red;}
</style>
<script>
$(function(){
var img = new Image();
img.onload = function(){
ctx.drawImage(img, 0,0);
};
img.src = "http://images.christmastimeclipart.com/images/2/1271716593176_1788/img_1271716593176_17881.jpg";
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var canvasOffset=$("#canvas").offset();
var offsetX=canvasOffset.left;
var offsetY=canvasOffset.top;
var canvasWidth=canvas.width;
var canvasHeight=canvas.height;
var isDragging=false;
// functions to handle mouseup, mousedown, mousemove, mouseout events
$("#canvas").mousedown(function(e){handleMouseDown(e);});
$("#canvas").mousemove(function(e){handleMouseMove(e);});
$("#canvas").mouseup(function(e){handleMouseUp(e);});
$("#canvas").mouseout(function(e){handleMouseOut(e);});
}); // end $(function(){});
</script>
</head>
<body> …Run Code Online (Sandbox Code Playgroud) 我在网页上设置了一个画布,其中运行着js动画,该动画填充了整个浏览器窗口,但是当我调整窗口的大小时,似乎无法自动调整画布的大小以适应新的窗口大小。有人可以告诉我我需要添加到我的js中才能正确获得此功能吗?谢谢!
var canvas = document.getElementById('canvas');
var makecodeplay = canvas.getContext('2d');
canvas.width=window.innerWidth;
canvas.height=window.innerHeight;
makecodeplay.clearRect(0, 0, canvas.width, canvas.height);
makecodeplay.fillStyle = "rgb(75,77,81)";
makecodeplay.fillRect(0, 0, canvas.width, canvas.height);
function randomPaint(inX, inY) {
var x = Math.floor(Math.random() * canvas.width);
var y = Math.floor(Math.random() * canvas.height);
var r, g, b;
r = Math.floor(Math.random() * 255);
g = Math.floor(Math.random() * 255);
b = Math.floor(Math.random() * 255);
makecodeplay.beginPath();
makecodeplay.fillStyle = "rgba(35,37,41,0.3)";
makecodeplay.fillRect(0, 0, canvas.width, canvas.height);
makecodeplay.fill();
makecodeplay.closePath();
makecodeplay.beginPath();
makecodeplay.strokeStyle = "rgba(" + r + "," + g + "," + …Run Code Online (Sandbox Code Playgroud)