这是我想在 Vue.js 中实现的行为这是我试图制作的 Js 小提琴示例: https: //jsfiddle.net/richardcwc/ukqhf54k/
//Canvas
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
//Variables
var canvasx = $(canvas).offset().left;
var canvasy = $(canvas).offset().top;
var last_mousex = last_mousey = 0;
var mousex = mousey = 0;
var mousedown = false;
//Mousedown
$(canvas).on('mousedown', function(e) {
last_mousex = parseInt(e.clientX-canvasx);
last_mousey = parseInt(e.clientY-canvasy);
mousedown = true;
});
//Mouseup
$(canvas).on('mouseup', function(e) {
mousedown = false;
});
//Mousemove
$(canvas).on('mousemove', function(e) {
mousex = parseInt(e.clientX-canvasx);
mousey = parseInt(e.clientY-canvasy);
if(mousedown) {
ctx.clearRect(0,0,canvas.width,canvas.height); //clear canvas
ctx.beginPath();
var …Run Code Online (Sandbox Code Playgroud) feColorMatrix我在画布 2D 渲染上下文中使用 SVG 过滤器。我希望能够动态更新矩阵的values属性,以动态更改颜色映射。但是,当我使用setAttribute更新矩阵时,它对画布的后续绘制没有影响。
下面的示例重现了该问题。矩阵最初交换红色和绿色通道,并且正确应用此过滤器,以便左侧的正方形绘制为绿色而不是红色。预期的结果是,将滤镜的values属性更改为redToBlue矩阵后,右侧的方块应该绘制为蓝色;实际结果是,尽管更新了矩阵值,右侧的方块仍保持绿色。
let redToBlue = '0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0';
let canvas = document.getElementById('canvas');
let ctx = canvas.getContext('2d');
let filter = document.getElementById('filter');
ctx.fillStyle = 'red';
ctx.filter = 'url(#filter)';
ctx.fillRect(10, 10, 80, 80);
filter.setAttribute('values', redToBlue);
ctx.fillRect(110, 10, 80, 80);Run Code Online (Sandbox Code Playgroud)
<svg>
<filter id="filter">
<feColorMatrix type="matrix" values="0 1 0 0 0 1 0 0 0 …Run Code Online (Sandbox Code Playgroud)不错的示例网格可以位于此站点中:https://playgameoflife.com,您可以在其中单击并在该网格中移动。所以我想学习如何制作这样一个无尽的网格,以便你可以继续前进。
片段: https: //jsfiddle.net/omar_red/wfsLuynd/1/
canvas = document.querySelector('.field');
ctx = canvas.getContext('2d');
canvas.width = window.screen.width;
canvas.height = window.screen.height;
for (let x = 0.5; x < canvas.width; x += 10) {
ctx.moveTo(x, 0);
ctx.lineTo(x, canvas.height);
}
for (let y = 0.5; y < canvas.height; y += 10) {
ctx.moveTo(0, y);
ctx.lineTo(canvas.width, y);
}
ctx.strokeStyle = "#888";
ctx.stroke();Run Code Online (Sandbox Code Playgroud)
body {
margin: 0;
padding: 0;
}Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Infinite Grid</title>
</head>
<body>
<canvas class="field"></canvas>
</body>
</html>Run Code Online (Sandbox Code Playgroud)
我正在尝试通过循环缩放方法来调整图形圆的大小。该循环将使圆的比例从 1 到 3,然后从 3 到 1 进行动画处理。下面的代码按照我的预期工作,但是,当圆放大时,它的大小会调整到右下角的方向。然后,在达到给定的最大比例后,圆的大小会调整回其相反方向(左上角)。这是代码:
const app = new PIXI.Application({
width: 300,
height: 300,
});
const canvasDiv = document.getElementById('canvasDiv');
canvasDiv.appendChild(app.view);
let circle = new PIXI.Graphics();
circle.beginFill(0xffffff);
circle.drawCircle(5, 5, 5);
circle.x = 150;
circle.y = 150;
circle.endFill();
app.stage.addChild(circle);
let scale = 1, motion = 1;
app.ticker.add(() => animateLoop());
function animateLoop(){
if (motion == 1){
if (scale < 3){
scale += 0.2;
}
else{
motion = 0;
}
}
else{
if (scale > 1){
scale -= 0.2;
}
else{ …Run Code Online (Sandbox Code Playgroud) 当我尝试将图像绘制到Canvas元素时,我得到了这个例外:
未捕获错误:INDEX_SIZE_ERR:DOM异常1
textureLoadedstaticsprite.js:14
StaticSpritestaticsprite.js:21
(匿名函数)
CanvasRenderingContent和HTMLImageElement都存在.我只是不明白:S
这是我正在使用的代码:
/**
* Class for drawing static sprites, like trees and blocks
*/
function StaticSprite(args) {
// Private Fields
var texture = new Image();
// Events
function textureLoaded(context) {
console.log(context);
console.log(texture);
context.drawImage(texture, 0, 0, 32, 32);
}
// Constructor
// Add event listeners
texture.addEventListener("load", textureLoaded(this.graphics), false);
// Load texture
texture.src = "img/assets/wall1.png";
if(args != undefined) {
// Set local fields
this.x = args.x || this.x;
this.y = args.y || this.y;
this.z = args.z || …Run Code Online (Sandbox Code Playgroud) 该页面有一个<img>标签和一个画布元素.内部的图像<img>被裁剪并使用放大镜进行操作.如何在画布内显示图像的裁剪内容.
我有一个canvas对象,可以从jQuery插件动态加载到我的页面上.它没有包装器,没有与之关联的id或类.但是之后我需要删除它
$(window).resize(function)() {...}
Run Code Online (Sandbox Code Playgroud)
发生了.我尝试过使用jQuery
...next().remove();
Run Code Online (Sandbox Code Playgroud)
技术,以便相邻的div元素可以从DOM中删除它,但我遇到了问题.特别是,我的页面上的其他元素也被删除.对此有健康的解决方法吗?
谢谢!
事实:以下代码有效.
var img = new Image();
img.onload = function() {
context.drawImage(img, 32, 32);
};
img.src = "example.png";
Run Code Online (Sandbox Code Playgroud)
第一次观察:以下内容不会画到画布上.
var img = new Image();
img.src = "example.png";
context.drawImage(img, 32, 32);
Run Code Online (Sandbox Code Playgroud)
第二个观察:以下将绘制到画布(最终)...
var img = new Image();
img.src = "example.png";
setInterval(function() {context.drawImage(img, 32, 32);}, 1000);
Run Code Online (Sandbox Code Playgroud)
为什么我需要在回调上调用drawImage函数?如果是这种情况,为什么它最终在嵌套在setInterval函数中时有效?
HTML canvas元素令人印象深刻,人们正在做的事情令人兴奋.当我研究开发人员使用的javascript时,如果我所看到的内容符合"WebGL"这个术语,那么它并不总是很明显.什么是WebGL和不是WebGL之间的界线在哪里?
关于如何使用FileSaver.js保存svg画布的任何详细示例?
我在https://github.com/eligrey/FileSaver.js上遵循了画布示例, 但是我在line83上看到了一个“未定义的不是函数”,这是一个空行。
看看我的示例代码...
<!doctype html>
<meta charset="utf-8">
<link rel="stylesheet" href="dagre_example.css">
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="./dagre-d3.js"></script>
<script src="./Blob.js"></script>
<script src="./canvas-toBlob.js"></script>
<script src="./FileSaver.js"></script>
<h1>Dagre D3 Demo: Sentence Tokenization</h1>
<svg id="svg-canvas" width=960 height=600></svg>
<script id="js">
// Create the input graph
var g = new dagreD3.graphlib.Graph()
.setGraph({})
.setDefaultEdgeLabel(function() { return {}; });
// Here we"re setting nodeclass, which is used by our custom drawNodes function
// below.
g.setNode(0, { label: "TOP", class: "type-TOP" });
g.setNode(1, { label: "S", class: "type-S" }); …Run Code Online (Sandbox Code Playgroud)