简体   繁体   English

使用图像和文本设置画布的背景色

[英]setting background color of a canvas with image and text

I want to create one canvas element where I want to put an image and some text under the image. 我想创建一个画布元素,在其中要放置图像和图像下的一些文本。 This thing is working fine. 这东西工作正常。 Now I want to set the background color of the canvas as 'yellow' and text color as 'black'. 现在,我想将画布的背景颜色设置为“黄色”,并将文本颜色设置为“黑色”。 The text color I can set but I could not able to set the background color of the canvas element. 我可以设置文本颜色,但无法设置canvas元素的背景颜色。 Here is my pen 这是我的

 function convertImageToCanvas() { var s = '<img src="http://usabilitygeek.com/wp-content/uploads/2012/04/HTML-Guidelines-Usability-SEO-A-Link-Tag.jpg" alt="" id="image-on" />'; var htmlObject = document.createElement('div'); htmlObject.innerHTML = s; var image = htmlObject.firstChild; var c = '<canvas id= "drawing"> </canvas>'; var canvasObject = document.createElement('div'); canvasObject.innerHTML = c; var canvas = canvasObject.firstChild; /*document.createElement("canvas");*/ var text = 'All the world \\'sa stage, and all the men and women merely players. They have their exits and their entrances; And one man in his time plays many parts.'; var lineHeight = 25; var maxWidth = image.naturalWidth; var context = canvas.getContext("2d"); var lines = getLines(context, text, maxWidth); canvas.width = image.naturalWidth; canvas.height = image.naturalHeight + lineHeight + (lines * lineHeight); canvas.style.background = "#FFFF00"; context.font = '16pt Calibri'; context.fillStyle = '#FFFF00'; //context.fill(); //var context = canvas.getContext("2d"); context.drawImage(image, 0, 0); var y = image.naturalHeight + lineHeight; var x = 0; wrapText(context, text, x, y, maxWidth, lineHeight); /*context.fillText("Here will be the comment", x, y);*/ var image = new Image(); image.src = canvas.toDataURL("image/png"); image.setAttribute('crossOrigin', 'anonymous'); console.log("the source of the image will be " + image.src); /*var imageUrl = canvas.toDataURL(); console.log("the image url is as followes"+imageUrl);*/ return canvas; } function wrapText(context, text, x, y, maxWidth, lineHeight) { var words = text.split(' '); var line = ''; for (var n = 0; n < words.length; n++) { var testLine = line + words[n] + ' '; var metrics = context.measureText(testLine); var testWidth = metrics.width; if (testWidth > maxWidth && n > 0) { context.fillText(line, x, y); line = words[n] + ' '; y += lineHeight; } else { line = testLine; } } context.fillText(line, x, y); //context.strokeText(line, x, y); } function getLines(context, text, maxWidth) { var words = text.split(' '); var line = ''; var num = 1; for (var n = 0; n < words.length; n++) { var testLine = line + words[n] + ' '; var metrics = context.measureText(testLine); var testWidth = metrics.width; if (testWidth > maxWidth && n > 0) { line = words[n] + ' '; num += 1; } else { line = testLine; } } return num; } 
 canvas { background: #FFFF00; } 
 <div id="total-container"> <div> Open dev tools then Click on the "Click Me" button. then check out the console tab and click on the link for the Image. </div> <div id="image-container"> <img src="http://usabilitygeek.com/wp-content/uploads/2012/04/HTML-Guidelines-Usability-SEO-A-Link-Tag.jpg" alt="" id="image-on" /> </div> <div id="comment-container"> Lorem Ipsum </div> <button onclick="convertImageToCanvas()">Click Me</button> 

You can set the background color of the canvas like this: 您可以像这样设置画布的背景色:

canvas{
    background-color: #FFFF00;
}

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

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