热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在CSS/Jquery中创建渐变网格-CreatingagradientmeshinCSS/Jquery

Imnotsureifthisispossibleornot,butcanIuseCSSJquerytechniquestocreateagradientme

I'm not sure if this is possible or not, but can I use CSS/Jquery techniques to create a gradient mesh? Something similar to this enter image description here

我不确定这是否可行,但我可以使用CSS / Jquery技术来创建渐变网格吗?类似的东西

I'd like to randomly generate this mesh and then possibly animate it, so I'm trying to avoid using images. I'm not sure if something like this would even be possible.

我想随机生成这个网格然后可能为它设置动画,所以我试图避免使用图像。我不确定这样的事情是否可行。

I was thinking maybe creating several layers of individual gradients and then layering them all together into a fixed position and changing their opacity settings?

我在考虑创建几层单独的渐变,然后将它们一起分层到一个固定的位置并改变它们的不透明度设置?

3 个解决方案

#1


4  

At present

I experimented with something along these lines a few years ago, using SVG, the HTML5 canvas tag, and more recently CSS3 gradients. I don't believe there's a natural way to go beyond simple linear or radial gradients at present.

几年前,我使用SVG,HTML5画布标签以及最近的CSS3渐变来尝试这些方面。我不相信目前有一种超越简单线性或径向渐变的自然方法。

The question is if a mesh gradient can be simulated using only simple linear and radial gradients (which are the only tools available).

问题是,是否可以仅使用简单的线性和径向渐变(这是唯一可用的工具)来模拟网格渐变。

Unfortunately, when you combine multiple gradients using opacity or rgba, the colors of the different gradients tend to combine in a way that's not useful, leading to washed-out colors. Avoiding this would require being able to render it in the browser as a single complex gradient.

不幸的是,当你使用不透明度或rgba组合多个渐变时,不同渐变的颜色倾向于以一种无用的方式组合,从而导致褪色。避免这种情况需要能够在浏览器中将其作为单个复杂渐变进行渲染。

There are also significant limitations to the shapes the gradients can have -- linear gradients at an arbitrary angle, and elliptical gradients with radial symmetry. Neither allows for free-form, irregular shapes. The 2D transformations that can be applied to the resulting image are fairly regular in nature as well (scaling, skewing, etc).

梯度可以具有显着的局限性 - 任意角度的线性梯度和径向对称的椭圆梯度。也不允许自由形状的不规则形状。可以应用于结果图像的2D变换在性质上也是相当规则的(缩放,倾斜等)。

In the future

在将来

The most promising option I'm aware of for the near future is the possible support for mesh gradients in SVG 2.0 (and perhaps diffusion curves as well). If this does happen and it's eventually supported by browsers, that should start to greatly expand the options. And the HTML5 canvas tag and CSS3 may follow soon afterwards.

我不知道在不久的将来最有希望的选择是可能支持SVG 2.0中的网格渐变(也可能是扩散曲线)。如果确实发生这种情况并且最终得到浏览器的支持,那么应该开始大大扩展选项。之后很快就会出现HTML5 canvas标签和CSS3。

And as @vals pointed out in the comment below, WebGL should offer some very promising options (for HTML5 canvas tags using a 3D context).

正如@vals在下面的评论中指出的那样,WebGL应该提供一些非常有前景的选项(对于使用3D上下文的HTML5画布标签)。

Related links

  • http://www.svgopen.org/2011/papers/18-Advanced_Gradients_for_SVG/
  • http://w3-org.9356.n7.nabble.com/Diffusion-Curves-td146288.html

#2


4  

I have done a simple layout to demostrate this.

我做了一个简单的布局来说明这一点。

First, I will put 4 divs, the first to show the partial results, and the last to see the final result. The markup is just:

首先,我将放置4个div,第一个显示部分结果,最后一个显示最终结果。标记只是:

here box is just for dimensions, mesh1 to 3 hold the partial results, in mesh we put it all together.

这里的框仅用于尺寸,mesh1到3保持部分结果,在网格中我们将它们全部放在一起。

The CSS is:

CSS是:

.box {
    width: 400px;
    height: 150px;
    position: relative;
    display: inline-block;
}

.mesh1, .mesh {
background:
    -webkit-linear-gradient(5deg,  rgba(0, 250, 0, 0.5), rgba(0, 100, 0, 0.5))
}

.mesh:after, .mesh:before {
    content: "";
    position: absolute;
    left: 0px;
    bottom: 0px;
    top: 0px;
    right: 0px;
}
.mesh2, .mesh:after {
background:   -webkit-radial-gradient(center center, circle cover, rgba(250, 0, 0, 0.6) 0%, rgba(120, 0, 10, 0.5) 100%);}

.mesh3, .mesh:before {
background: -webkit-radial-gradient(10% 10%, ellipse cover, rgba(0, 0, 250, 0.6) 0%, white 100%);}

I am giving to the mesh1 class a background linear, inclinated 5 degrees, and specifying colors in rgba format to allow for transparency.

我给mesh1类一个背景线性,倾斜5度,并指定rgba格式的颜色以允许透明度。

Then, to be able to overlay more gradients, I specify to pseudo elements as before and after, sharing the same layout properties.

然后,为了能够覆盖更多的渐变,我指定了前后的伪元素,共享相同的布局属性。

To the after element I give a circular gradient, shared with the mesh2 To the before element I give an elliptical gradient, off center. All of them can be rgba.

对于after元素,我给出一个圆形渐变,与mesh2共享。对于before元素,我给出一个椭圆渐变,偏离中心。所有这些都可以是rgba。

At the end, you see in the mesh div the result of overlapping everything

最后,您在网格div中看到重叠所有内容的结果

(I have used everywhere the webkit notation to make it short)

(我已经到处使用webkit表示法来缩短它)

I wouldn't say it is much artistic, but I leave this part to you :-)

我不会说这很有艺术性,但我把这部分留给你:-)

fiddle here

#3


2  

In my first answer, I interpreted this going more in the "artistic" way than in the "mathematical" way. The answer from Matt Coughlin make me think about a more mathematic answer, in wich we would make the "mesh" part of the requirement more rigorous.

在我的第一个答案中,我认为这更多地是以“艺术”方式而不是“数学”方式。 Matt Coughlin的答案让我想到了一个更加数学化的答案,我们会把这个要求的“网格”部分更加严谨。

That is, we have a matrix of colors, and we want to show this in a grid. If the grid has a spacing of say 100px, then the color [x][y] of the matrix would be given to the pixel in 100x and 100y. The pixels in between would be aproximated in a bilinear way.

也就是说,我们有一个颜色矩阵,我们希望在网格中显示它。如果网格的间距为100px,则矩阵的颜色[x] [y]将以100x和100y的形式给予像素。中间的像素将以双线性方式进行近似。

For such an approach, the css would be:

对于这种方法,css将是:

.mesh { overflow: hidden; position: absolute;   width: 300px;   height: 300px;    }

.tile {    width: 200px;    height: 200px;    position: absolute;    display: block;   }

.tile11, .tile21, .tile31 {
left: -50px;
}
.tile12, .tile22, .tile32 {
left: 50px;
}
.tile13, .tile23, .tile33 {
left: 150px;
}
.tile11, .tile12, .tile13 {
top: -50px;
}
.tile21, .tile22, .tile23 {
    top: 50px;
}
.tile31, .tile32, .tile33 {
top: 150px;
}

.tile11 {
background: -webkit-radial-gradient(center center, 100px 100px, 
      rgba(255, 0, 0, 1) 0%, 
      rgba(255, 0, 0, 0.5) 50%,
      rgba(255, 0, 0, 0) 100%);}

.tile12 {
background: -webkit-radial-gradient(center center, 100px 100px, 
      rgba(255, 0, 0, 1) 0%, 
      rgba(255, 0, 0, 0.5) 50%,
      rgba(255, 0, 0, 0) 100%);}

I am using every div as a local contributor of the mesh, getting only to "touch" the inmediate neighbour, and going to a full transparency beyond that point.

我使用每个div作为网格的本地贡献者,只是“触摸”中间邻居,并在此之后达到完全透明。

The result is this :

结果如下:

fiddle

The 2 first colors are both red as a test. In a perfect system, the line connecting the 2 points should be perfect red all the time.

2种第一种颜色都是红色的测试。在一个完美的系统中,连接2个点的线应始终是完美的红色。

It's true that it's not a perfect result, but the "muddying" or "washyness" of the result is more or less avoided

确实,这不是一个完美的结果,但结果的“混乱”或“淡态”或多或少地避免了


推荐阅读
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
    本文介绍了Android Studio Bumblebee | 2021.1.1(大黄蜂版本)的使用方法和相关知识,包括Gradle的介绍、设备管理器的配置、无线调试、新版本问题等内容。同时还提供了更新版本的下载地址和启动页面截图。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 深度学习中的Vision Transformer (ViT)详解
    本文详细介绍了深度学习中的Vision Transformer (ViT)方法。首先介绍了相关工作和ViT的基本原理,包括图像块嵌入、可学习的嵌入、位置嵌入和Transformer编码器等。接着讨论了ViT的张量维度变化、归纳偏置与混合架构、微调及更高分辨率等方面。最后给出了实验结果和相关代码的链接。本文的研究表明,对于CV任务,直接应用纯Transformer架构于图像块序列是可行的,无需依赖于卷积网络。 ... [详细]
  • Android日历提醒软件开源项目分享及使用教程
    本文介绍了一款名为Android日历提醒软件的开源项目,作者分享了该项目的代码和使用教程,并提供了GitHub项目地址。文章详细介绍了该软件的主界面风格、日程信息的分类查看功能,以及添加日程提醒和查看详情的界面。同时,作者还提醒了读者在使用过程中可能遇到的Android6.0权限问题,并提供了解决方法。 ... [详细]
  • 本文介绍了Windows Vista操作系统中的用户账户保护功能,该功能是为了增强系统的安全性而设计的。通过对Vista测试版的体验,可以看到系统在安全性方面的进步。该功能的引入,为用户的账户安全提供了更好的保障。 ... [详细]
  • OpenMap教程4 – 图层概述
    本文介绍了OpenMap教程4中关于地图图层的内容,包括将ShapeLayer添加到MapBean中的方法,OpenMap支持的图层类型以及使用BufferedLayer创建图像的MapBean。此外,还介绍了Layer背景标志的作用和OMGraphicHandlerLayer的基础层类。 ... [详细]
  • Summarize function is doing alignment without timezone ?
    Hi.Imtryingtogetsummarizefrom00:00otfirstdayofthismonthametric, ... [详细]
  • Linux服务器密码过期策略、登录次数限制、私钥登录等配置方法
    本文介绍了在Linux服务器上进行密码过期策略、登录次数限制、私钥登录等配置的方法。通过修改配置文件中的参数,可以设置密码的有效期、最小间隔时间、最小长度,并在密码过期前进行提示。同时还介绍了如何进行公钥登录和修改默认账户用户名的操作。详细步骤和注意事项可参考本文内容。 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • 本文介绍了如何使用Express App提供静态文件,同时提到了一些不需要使用的文件,如package.json和/.ssh/known_hosts,并解释了为什么app.get('*')无法捕获所有请求以及为什么app.use(express.static(__dirname))可能会提供不需要的文件。 ... [详细]
  • 本文介绍了一个适用于PHP应用快速接入TRX和TRC20数字资产的开发包,该开发包支持使用自有Tron区块链节点的应用场景,也支持基于Tron官方公共API服务的轻量级部署场景。提供的功能包括生成地址、验证地址、查询余额、交易转账、查询最新区块和查询交易信息等。详细信息可参考tron-php的Github地址:https://github.com/Fenguoz/tron-php。 ... [详细]
  • 本文介绍了Python函数的定义与调用的方法,以及函数的作用,包括增强代码的可读性和重用性。文章详细解释了函数的定义与调用的语法和规则,以及函数的参数和返回值的用法。同时,还介绍了函数返回值的多种情况和多个值的返回方式。通过学习本文,读者可以更好地理解和使用Python函数,提高代码的可读性和重用性。 ... [详细]
author-avatar
单身男人adgjm
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有