paper.js隐藏/显示图层?

时间:2012-05-29 14:51:24

标签: paperjs

我有一个包含很多符号的图层,我想隐藏并显示整个图层的所有元素。

隐藏我使用myLayer.remove();执行此操作,但要显示它没有方法...
在他们的教程中,他们说project.activeLayer.addChild(myObject);但它似乎不适用于图层。 (http://paperjs.org/tutorials/project-items/project-hierarchy/)

如果有人可以帮助我,或者告诉我是否需要采取不同的方式?

非常感谢。

2 个答案:

答案 0 :(得分:8)

调用Layer.remove()时,将从project.layers数组中删除该Layer实例。要重新显示已删除的图层(以及其中的任何对象),请将其推回到project.layers。

var blueSquare = Path.Rectangle(new Point(0, 0), new Size (50, 50));
blueSquare.fillColor = 'blue';

var newLayer = new Layer();
newLayer.activate();    // so that redCircle will be added to newLayer
var redCircle = Path.Circle(new Point(100, 100), 50);
redCircle.fillColor = 'red';

newLayer.remove();      // this prevents the redCircle from being drawn
project.layers.push(newLayer);  // now the redCircle is back

或者,代替newLayer.remove();,您可以使用newLayer.visible = false;newLayer.opacity = 0;,以便newLayer永远不会从project.layers数组中删除,尽管使用这些方法选择的项目仍会显示线框即使实际项目无法看到。

答案 1 :(得分:0)

对于图层,有一个称为 visibility 的属性。您可以将其设置为true / false。

"""
Docstring.
"""

import chess
import chess.svg

from PyQt5.QtCore import pyqtSlot
from PyQt5.QtSvg import QSvgWidget

COORDINATES = True
FLIPPED = False


class Chessboard(QSvgWidget):
    """
    Docstring.
    """

    def __init__(self):
        """
        Docstring.
        """
        super().__init__()

        self.clicked_square = -20
        self.move_from_square = -20
        self.move_to_square = -20
        self.piece_to_move = [None, None]

        viewbox_size = 400
        self.margin = chess.svg.MARGIN * 800 / viewbox_size if COORDINATES else 0
        self.file_size = chess.svg.SQUARE_SIZE * 800 / viewbox_size
        self.rank_size = chess.svg.SQUARE_SIZE * 800 / viewbox_size

        self.chessboard = chess.Board()
        self.draw_chessboard()

    @pyqtSlot(QSvgWidget)
    def mousePressEvent(self, event):
        """
        Docstring.
        """
        x_coordinate = event.x()
        y_coordinate = event.y()

        file = int(x_coordinate / 800 * 8)
        rank = 7 - int(y_coordinate / 800 * 8)

        if file < 0:
            file = 0

        if rank < 0:
            rank = 0

        if file > 7:
            file = 7

        if rank > 7:
            rank = 7

        self.clicked_square = chess.square(file,
                                           rank)
        piece = self.chessboard.piece_at(self.clicked_square)

        file_character = chr(file + 97)
        rank_number = str(rank + 1)
        ply = f"{file_character}{rank_number}"

        if self.piece_to_move[0]:
            move = chess.Move.from_uci(f"{self.piece_to_move[1]}{ply}")
            if move in self.chessboard.legal_moves:
                self.chessboard.push(move)

                self.move_from_square = move.from_square
                self.move_to_square = move.to_square

                piece = None
                ply = None

        self.piece_to_move = [piece, ply]
        self.draw_chessboard()

    def draw_chessboard(self):
        """
        Docstring.
        """
        is_it_check = self.chessboard.king(self.chessboard.turn) \
                      if self.chessboard.is_check() \
                      else None
        self.svg_chessboard = chess.svg.board(board=self.chessboard,
                                              lastmove=chess.Move(from_square=self.move_from_square,
                                                                  to_square=self.move_to_square),
                                              arrows=[(self.clicked_square, self.clicked_square),
                                                      (self.move_from_square, self.move_to_square)],
                                              check=is_it_check,
                                              flipped=FLIPPED,
                                              coordinates=COORDINATES,
                                              size=800)
        self.svg_chessboard_encoded = self.svg_chessboard.encode("utf-8")
        self.load(self.svg_chessboard_encoded)

现在,当您创建新图层时,可以在对象上调用show或hide方法来更改可见性。

paper.Layer.prototype.hide = function(){
   this.visibility = false
}

paper.Layer.prototype.show = function(){
   this.visibility = true
}
  

但是,这并不会从活动层中删除该层。   您所做的任何更改都将位于活动层中,即使其   隐藏。

为此,我将一层称为。 rootLayer 并将其隐藏时设置为活动状态(baseLayer = new Layer() // hiding layer baseLayer.hide() //showing layer baseLayer.show() )。这是一个hack,但可以完成工作