jquery - 如何在照片上嵌入文字

标签 jquery html css

我想将文本嵌入到 div 中的照片中。因此,当有人保存该照片时,它应该与文字一起保存。我试过使用下面的脚本。但是,我无法得到我所期望的。

It give an error on ctx = canvas.getContext('2d'), line as undefined is not a function.

HTML:

<div id="img_canvas" class="img_canvas" style="background: url('<?= base_url();?>uploads/x-meme-1.0.jpg')no-repeat;width: 100%!important;height: 663px;background-position: 50% 50%;background-size: contain;"></div>

<input type="button" id="add_meme" style="cursor: pointer;" class="right add_meme" value="Submit">

这是我的脚本:

$(document).ready(function() {
        $('#add_meme').on('click', function(ev) {

            var canvas = document.getElementById('img_canvas'),
                ctx = canvas.getContext('2d'),
                reader = new FileReader;
            reader.onload = function(event) {
                var img = new Image;

                img.onload = function() {
                    canvas.width = this.width;
                    canvas.height = this.height;
                    ctx.drawImage(this, 0, 0);
                    ctx.font = "36pt Verdana";

                    ctx.fillStyle = "red";
                    ctx.fillText("Test Text", 42, 82);

                    ctx.fillStyle = "white";
                    ctx.fillText("Test Text", 40, 80);

                };

                img.src = event.target.result;
            };

            reader.readAsDataURL(ev.originalEvent.target.files[0]);

        });
    });

最佳答案

HTML

<div id="img_canvas" class="img_canvas" style="background: url('<?= base_url();?>uploads/x-meme-1.0.jpg')no-repeat;width: 100%!important;height: 663px;background-position: 50% 50%;background-size: contain;"></div>

更改 HTML

<canvas id="img_canvas" class="img_canvas" style="background: url('<?= base_url();?>uploads/x-meme-1.0.jpg')no-repeat;width: 100%!important;height: 663px;background-position: 50% 50%;background-size: contain;"></canvas>

canvas.getContext and you are fetching from div.

关于jquery - 如何在照片上嵌入文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29406164/

相关文章:

名称滚动位置上的 Jquery addClass

jquery - 在动态加载的内容上使用hammer.js

javascript - jQuery 对点击事件的绑定(bind)

javascript - Gridster.js 删除小部件

php - 当用户点击播放时在同一个网页上弹出视频

css - 在 Internet Explorer 中显示左侧和底部的边框如何删除该边框?

css - IE7 和 8 忽略设置为标题元素的最大宽度

jquery,选择器,查找,文本

html - 在 IE 中悬停后下拉立即消失

html - 网站自动缩小? CSS 更改不会出现