【问题标题】:Blur image with pixastic and set it as background with jquery使用 pixastic 模糊图像并使用 jquery 将其设置为背景
【发布时间】:2014-01-20 11:16:17
【问题描述】:

我有一个问题困扰我 2 天。

我正在开发一个小型音乐共享网站,但在为播放器设计时遇到了问题。

我想使用 CSS 将模糊的图像背景应用于 div,并通过 JS 开始将模糊应用于图像,所有这些都使用 pixastic js 库。问题是 pixastic 给了我一个 HTMLImageElement 女巫,我不能用$('.footer').css('background', "url(" + img.toDataURL("image/png")+ ")" }); 应用到css,因为 toDataURL 只适用于画布元素。 我该怎么做?

到目前为止我的代码是:

 var img = new Image();
    img.src = '<?php echo $hd ?>';
    img.onload = function() {
    Pixastic.process(img, "blurfast", {amount:1});
    }

    $('.footer').css('background', "url(" + img.toDataURL("image/png")+ ")" });

$hd 是一个 http url,它使用 lastfm API 指向专辑封面

【问题讨论】:

    标签: php javascript jquery html canvas


    【解决方案1】:

    我不确定 css 方法的第一个参数以及为什么不使用 img.src 作为图像的 url?尝试这样的事情: 罢工>

    $('.footer').css('background-image', 'url(' + img.src + ')');
    

    好的,我在文档中找到了可能的答案:

    var options = {};
    Pixastic.process(image, "action", options);
    options.resultCanvas; // <- holds new canvas
    

    所以你的情况是:

    var img = new Image(),
        options = {};
    
    options.amount = 1
    img.src = '<?php echo $hd ?>';
    img.onload = function() {
        Pixastic.process(img, "blurfast", options);
        $('.footer').css('background', "url(" + options.resultCanvas.toDataURL("image/png")+ ")" });
    }
    

    【讨论】:

    • 问题是,如果我这样做img.src,我只是取回原始图像 url,没有模糊效果,如果可以的话,我要求一种方法来获取模糊图像的 url ,或将图像转换为画布
    • 而且你不能把图片放在背景中作为图片,位置绝对z-index -1等来模拟背景
    • 什么意思?将模糊的画布(图像)放在 div 上,然后设置它的 z-index?
    • 嗯,我收到了Uncaught TypeError: Cannot call method 'toDataURL' of undefined 错误,:/
    • 我在 onload 方法中使用 css 方法移动了行。现在试试。也看看optionsconsole.log是什么
    猜你喜欢
    • 1970-01-01
    • 2020-02-04
    • 2021-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多