简体   繁体   中英

Drawing a blurred circle in (QML/Javascript) canvas

圆圈模糊

How can we draw a blurred circle in canvas using QML or Javascript?

I have tried:

var ctx = getContext("2d");
ctx.save();
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
ctx.shadowBlur = 20;
ctx.shadowColor = "#22ff0000";
ctx.shadowOffsetX = 10;
ctx.lineWidth = 1;
ctx.strokeStyle = "red";
ctx.arc(canvas.centerWidth,
  canvas.centerHeight,
  canvas.radius, canvas.angleOffset,
  canvas.angleOffset + canvas.angle);
ctx.stroke();
ctx.restore();

But my problem is the strong line is visible. I need to draw it smoothly without edges. How to do this?

import QtQuick 2.7
import QtQuick.Window 2.0
import QtGraphicalEffects 1.0

Window {
    id:container
    width: 600
    height: 300
    visible: true

    Rectangle {
        anchors.fill: parent
        gradient: Gradient {
            GradientStop { position: 0.0; color: "#F0F0F0" }
            GradientStop { position: 0.5; color: "#FFFFFF" }
        }
        Row {
            anchors.fill: parent

            RadialGradient {
                width: 300
                height: 300
                gradient: Gradient {
                    GradientStop { position: 0.25; color: "#00000000" }
                    GradientStop { position: 0.3; color: "#80F5A9E1" }
                    GradientStop { position: 0.35; color: "#00000000" }
                }

            }

            Canvas {
                id: canvas
                width: 300
                height: 300
                onPaint: {
                    var context = getContext('2d');
                    context.rect(0, 0, canvas.width, canvas.height);
                    var grd = context.createRadialGradient(150, 150, 0, 150, 150, 300);
                    grd.addColorStop(0.25, '#00000000');
                    grd.addColorStop(0.30, '#80F5A9E1');
                    grd.addColorStop(0.35, '#00000000');
                    context.fillStyle = grd;
                    context.fill();
                }
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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