简体   繁体   English

如何在画布对象jQuery中添加类

[英]How to add class inside canvas object jQuery

I work with canvas, and i have issues, i want to create layout with canvas objects.我使用画布,但有问题,我想用画布对象创建布局。 But i want to create object in css, and create in HTML.但我想在 css 中创建对象,并在 HTML 中创建。

For now i create:现在我创建:

var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');

context.beginPath();


context.rect(10, 50, 200, 100);
context.fillStyle = 'red';
context.fill();
context.fillStyle = 'black';
context.font = '20px Courier';
context.shadowColor = 'transparent';
context.shadowBlur = 0;
context.shadowOffsetX = 0;
context.shadowOffsetY = 0;
context.fillText(`List item`, 70, 80);
context.fillText(`List item2`, 70, 130);

var image = new Image();
image.src = "https://pngimage.net/wp-content/uploads/2018/06/machine-icon-png-9.png";
image.onload = function () {
    context.drawImage(image, 10, 70, 50, 50);
};

CSS: CSS:

.box-test {
    width: 150px;
    height: 100px;
    background-color: #fff;
}

HTML: HTML:

 <canvas id="myCanvas" width="1280" height="720"
            style="border:1px solid #c3c3c3;">
        Your browser does not support the canvas element.
    </canvas>

This code are build boxes with image and text, but color, image i want to get from CSS, somehow it's possible to create object in canvas use css styles?此代码是带有图像和文本的构建框,但是我想从 CSS 中获取颜色、图像,不知何故可以使用 css 样式在画布中创建对象?

As u can see i have class box-test and here i give properties to box:正如你所看到的,我有类box-test ,在这里我为 box 提供了属性:

context.rect(10, 50, 200, 100);
    context.fillStyle = 'red';
    context.fill();

Can i use only cordinates here, and width, height, color get from css?我可以在这里只使用坐标,宽度、高度、颜色来自 css 吗?

If you use SVG you can on the fly add elements to it如果您使用 SVG,您可以即时向其添加元素

 var canvas = document.getElementById("svgArea"); var rec = document.createElementNS("http://www.w3.org/2000/svg","rect") rec.setAttributeNS(null, "class","box-test"); canvas.appendChild(rec); var image = document.createElementNS("http://www.w3.org/2000/svg","image") image.setAttributeNS(null, "href","https://pngimage.net/wp-content/uploads/2018/06/machine-icon-png-9.png"); image.setAttributeNS(null, 'height', '50'); image.setAttributeNS(null, 'width', '50'); image.setAttributeNS(null, 'x', 60); image.setAttributeNS(null, 'y', 60); canvas.appendChild(image ); var text = document.createElementNS("http://www.w3.org/2000/svg","text"); text.textContent= "Hello World!"; text.setAttributeNS(null, 'x', 120); text.setAttributeNS(null, 'y', 120); text.setAttributeNS(null, "class","text-style"); canvas.appendChild(text);
 .box-test { width: 50px; height: 50px; x: 20px; y: 20px; fill:rgb(0,0,255); stroke-width:3; stroke:rgb(0,0,0); } .text-style { font-size: 20px; font-family: Courier; }
 <svg id="svgArea" width="500" height="500"></svg>

To set X and Y coordinates then you can use this code要设置XY坐标,则可以使用此代码

rec.setAttributeNS(null, 'x', x);
rec.setAttributeNS(null, 'y', y);

To set width and height设置widthheight

rec.setAttributeNS(null, 'height', '50');
rec.setAttributeNS(null, 'width', '50'); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM