- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我已经模拟了我在 a JSFiddle 中尝试做的事情.
为了您的方便,这里是代码:
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var radius = 75;
var startAngle = 0// 1.1 * Math.PI;
var endAngle = 2.0 * Math.PI;//1.9 * Math.PI;
var counterClockwise = false;
context.beginPath();
var gradient = context.createLinearGradient(radius, radius, 0, 0);
gradient.addColorStop(0, '#3b749a');
gradient.addColorStop(1, '#FFFFFF');
context.lineWidth = 15;
context.arc(radius, radius, radius, startAngle, endAngle, counterClockwise);
context.strokeStyle = gradient;
context.stroke(gradient);
现在这是我的挑战。无论我尝试什么,我似乎都无法让渐变跟随路径。它似乎可以从任何它想要的 Angular 应用,而不是遵循路径。我做错了什么?
其次,是否可以像模型一样,在一个边上做一个圆 Angular ?
所以看起来没有办法用渐变描边路径,只能将渐变应用到路径“切出”的 Canvas 上。 (而且 HTML5 中没有“圆形”渐变 AFAIK。)
由于建议的解决方案需要绘制两个元素,因此旋转/动画化的最佳方式是什么?对于一个对象/上下文,我似乎可以捕获它,进行变换和旋转。有了两个,我是否需要渲染到图像对象并旋转?
最佳答案
这实际上不是那么微不足道。也许有更简单的方法来做到这一点,我只是采取了艰难的方法。或者这可能是新事物。
我所做的是最终在圆圈内绘制了一个渐变框。在一个间隔上,圆改变它的起点和终点,这样间隙就会旋转。当发生这种情况时,我重新计算渐变框以适应新的间隙,然后在那里绘制它。结果是一个不错的效果。
在此演示中,我将其设置为随机循环显示不同的颜色和不同的尺寸,以展示不同的旋转器的外观并获得一些乐趣。
操作的核心在这个函数中:
function showSpinner(
startAngle,endAngle,direction,radius,line,context,color,shadow)
{
context.beginPath();
var a = startAngle / Math.PI;
a = a % 2;
a = 2 - a;
a *= Math.PI;
var x = radius + Math.cos(a)*radius*1.7;
var y = radius - Math.sin(a)*radius*1.7;
var gradient = context.createLinearGradient(radius,radius,x,y);
gradient.addColorStop(0.05, color);
gradient.addColorStop(.60, '#FFFFFF');
context.lineWidth = line;
context.lineCap = "round";
context.shadowBlur = 10;
if( shadow === true )context.shadowColor = '#727272';
context.arc(radius, radius, radius-line, startAngle, endAngle, direction);
context.strokeStyle = gradient;
context.stroke();
}
这个函数利用上面的绘图函数来提供动画
function spinner(obj){
var radius,line,color,shadow;
if( obj && obj.hasOwnProperty("shadow") ){
shadow = true;
}else{ radius = 75; }
if( obj && obj.hasOwnProperty("radius") ){
radius = obj.radius;
}else{ radius = 75; }
if( obj && obj.hasOwnProperty("line") ){
line = obj.line;
}else{ line = 7; }
var speed = {inc:0.04,loop:15};
if( obj && obj.hasOwnProperty("speed") ){
if( obj.speed == "slow" ){
speed = {inc:0.02,loop:25};
}
}
if( obj && obj.hasOwnProperty("color") ){
color = obj.color;
}else{ color = '#3b749a'; }
var canvas = document.getElementById('myCanvas');
canvas.style.height = 2*(radius+line) + "px";
canvas.style.width = 4*(radius+line) + "px";
var context = canvas.getContext('2d');
var startAngle,endAngle;
var counterClockwise = false;
var sa = 1.2;
var ea = 0.85;
var spinner = setInterval(function(){
canvas.width = canvas.width;
sa += speed.inc;
ea += speed.inc;
startAngle = sa * Math.PI;
endAngle = ea * Math.PI;
showSpinner(
startAngle,
endAngle,
counterClockwise,
radius,
line,
context,
color,
shadow
);
},speed.loop);
setTimeout(function(){ clearInterval(spinner);},15000);
return spinner;
}
但是要使用它,您真正需要做的就是
spinner()
如果你愿意,可以传入一个参数。您的选项是半径、线、颜色、阴影和速度。
var obj = {};
obj.line = int - the size in pixels of the line width
obj.radius = int - the radius of the circle drawn (currently maxed at 75 for viewing, but you can always change the size of the canvas if you want)
obj.color = string - the color that the line of the spinner will be
obj.shadow = bool - show the shadow or don't show it
obj.speed = string - Only supports "slow" right now. Otherwise it will be fast
关于javascript - 在圆上做旋转渐变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15899982/
...沮丧。我希望我的游戏仅在横向模式下运行。我已将适当的键/值添加到 Info.plist 文件中,以强制设备方向在启动时正确。 我现在正在尝试旋转 OpenGL 坐标空间以匹配设备的坐标空间。我正
我如何创建一个旋转矩阵,将 X 旋转 a,Y 旋转 b,Z 旋转 c? 我需要公式,除非您使用的是 ardor3d api 的函数/方法。 矩阵是这样设置的 xx, xy, xz, yx, yy, y
假设我有一个包含 3 个 vector 的类(一个用于位置,一个用于缩放,一个用于旋转)我可以使用它们生成一个变换矩阵,该矩阵表示对象在 3D 空间中的位置、旋转和大小。然后我添加对象之间的父/子关系
所以我只是在玩一个小的 javascript 游戏,构建一个 pacman 游戏。你可以在这里看到它:http://codepen.io/acha5066/pen/rOyaPW 不过我对旋转有疑问。你
在我的应用程序中,我有一个 MKMapView,其中显示了多个注释。 map 根据设备的航向旋转。要旋转 map ,请执行以下语句(由方法 locationManager 调用:didUpdateHe
使用此 jquery 插件时:http://code.google.com/p/jqueryrotate/wiki/Documentation我将图像旋转 90 度,无论哪个方向,它们最终都会变得模糊
我有以下代码:CSS: .wrapper { margin:80px auto; width:300px; border:none; } .square { widt
我只想通过小部件的轴移动图像并围绕小部件的中心旋转(就像任何数字绘画软件中的 Canvas ),但它围绕其左顶点旋转...... QPainter p(this); QTransform trans;
我需要先旋转图像,然后再将其加载到 Canvas 中。据我所知,我无法使用 canvas.rotate() 旋转它,因为它会旋转整个场景。 有没有好的JS方法来旋转图片? [不依赖于浏览器的方式] 最
我需要知道我的 Android 设备屏幕何时从一个横向旋转到另一个横向(rotation_90 到 rotation_270)。在我的 Android 服务中,我重新实现了 onConfigurati
**摘要:**本篇文章主要讲解Python调用OpenCV实现图像位移操作、旋转和翻转效果,包括四部分知识:图像缩放、图像旋转、图像翻转、图像平移。 本文分享自华为云社区《[Python图像处理] 六
我只是在玩MTKView中的模板设置;并且,我一直在尝试了解以下内容: 相机的默认位置。 使用MDLMesh和MTKMesh创建基元时的默认位置。 为什么轮换还涉及翻译。 相关代码: matrix_f
我正在尝试使用包 dendexend 创建一个树状图。它创建了非常好的 gg 树状图,但不幸的是,当你把它变成一个“圆圈”时,标签跟不上。我将在下面提供一个示例。 我的距离对象在这里:http://s
我想将一个完整的 ggplot 对象旋转 90°。 我不想使用 coord_flip因为这似乎会干扰 scale="free"和 space="free"使用刻面时。 例如: qplot(as.fac
我目前可以通过首先平移到轴心点然后执行旋转最后平移回原点来围绕轴心点旋转。在我的例子中,我很容易为肩膀做到这一点。但是,我不知道如何为前臂添加绕肘部的旋转。 我已经尝试了以下围绕肘部旋转的前臂: 平移
我想使用此功能旋转然后停止在特定点或角度。现在该元素只是旋转而不停止。代码如下: $(function() { var $elie = $("#bkgimg");
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 4 年前。 Improve this ques
我正在尝试创建一个非常简单的关键帧动画,其中图形通过给定的中点从一个角度旋转到另一个角度。 (目的是能够通过大于 180 度的 OBTUSE 弧角来制作旋转动画,而不是让动画“作弊”并走最短路线,即通
我需要旋转 NSView 实例的框架,使其宽度变为其高度,其高度变为其宽度。该 View 包含一个字符串,并且该字符串也被旋转,这一点很重要。 我查看了 NSView 的 setFrameRotati
我正在编写一个脚本,用于在 javascript 中旋转/循环浏览图像,同时遵守循环浏览图像的次数限制。我所拥有的如下: var delay = 3000; //6000 = change to
我是一名优秀的程序员,十分优秀!